B. *_*non 11 html javascript jquery
我想响应用户选择select元素中的项目.然而这个jQuery:
$('#platypusDropDown').select(function () {
alert('You selected something');
});
Run Code Online (Sandbox Code Playgroud)
...什么也没做.尽管jsFiddle将其视为有效的jQuery,但它没有显示警报.
Click事件有效,但速度太快 - 在进行选择之前点击select元素时会触发.
当然,我真的想做点什么:
$('#platypusDropDown').select(function () {
var selection = $('platypusDropDown').val;
$.getJSON('platypus.json', selection, data() {
// . . .
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<select id="platypusDropDown">
<option value="duckbill">duckbill</option>
<option value="duckbillPlatypus">duckbillPlatypus</option>
<option value="Platypus">Platypus</option>
<option value="Platypi">Platypi</option>
</select>
Run Code Online (Sandbox Code Playgroud)
PSL*_*PSL 10
您需要使用更改事件
$('#platypusDropDown').change(function () {
var selection = this.value; //grab the value selected
$.getJSON('platypus.json', selection, data() {
. . .
});
});
Run Code Online (Sandbox Code Playgroud)
另外$('platypusDropDown').val这里应该是val val(),你不需要在元素上再次创建一个jquery对象,this在处理程序内表示DOM元素(选择事件绑定到的那个),你可以直接访问该值this.value或者$(this).val()
| 归档时间: |
|
| 查看次数: |
25850 次 |
| 最近记录: |