绑定更改为下拉菜单 [英] Binding onchange to dropdown
本文介绍了绑定更改为下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很困惑,将 onchange
绑定到 select
。我可以看到有多种方法可以这样做。
html
< select id =ddlonchange =test()>< / select>
jquery
$(function(){
$(#ddl)。change(function(){
return test();
});
});
或
$(function(){
$(#ddl)bind(change,function(){
return test();
} );
});
在这3种方法中
- 哪一个被认为是标准做法?
- 这些方法有什么优点吗?
解决方案
您还可以使用 .on
< ('change',function(){
// code
}); pre>
$('SELECT'
在jQuery 1.7之前, change()
on()已替换 bind(change)
, so change()
是一个快捷方式。
所以最好的方法是;
$(SELECT)。bind(change,function(e){}); on(change,function(e){});
$(SELECT)。
我更喜欢第二个选项,因为它也可以自动应用于动态生成的 DOM
。
I am little confused in binding onchange
to select
. I can see there are multiple ways of doing this.
html
<select id="ddl" onchange="test()"></select>
jquery
$(function(){
$("#ddl").change(function(){
return test();
});
});
or
$(function(){
$("#ddl").bind("change", function(){
return test();
});
});
Among these 3 methods
- Which one is considered as a standard practice?
- Is there any merits with any of these methods?
解决方案
You can also use .on
$('SELECT').on('change',function(){
// code
});
Before jQuery 1.7, change()
was simply an short cut for bind("change")
.
As of 1.7 however, on()
has replaced bind("change")
, so change()
is a shortcut for that instead.
Therefore the best way would be either;
$("SELECT").bind("change", function(e) {});
$("SELECT").on("change", function(e) {});
I prefer the 2nd option as it also can applied automatically to the dynamically generated DOM
.
这篇关于绑定更改为下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文