jQuery 选择更改时用jQuery检测
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19528531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect with JQuery when a select changes
提问by user2876368
I have a Jqgrid which dinamically generates selects like this:
我有一个 Jqgrid,它动态生成这样的选择:
<select id="won" style="width: 65px;">
<option value="">-WON?</option>
<option value="1" selected>Bet1</option>
<option value="2" >Bet2</option>
<option value="3" >Bet3</option>
</select>
Each one have a different selected option. I would like to detect when one select changes so I can save it in my database.
每个都有不同的选择选项。我想检测一个选择何时更改,以便我可以将其保存在我的数据库中。
Im trying with:
我正在尝试:
$('#won').change(function(){
alert("PROBANDO");
});
But is not working at all. Any help will be apreciated Thanks.
但根本不起作用。任何帮助将不胜感激谢谢。
回答by Alessandro Minoccheri
The problem is that the select are created dynamically, then you need to use .on()
问题是select是动态创建的,那么你需要使用 .on()
try this:
尝试这个:
$(document).on('change','#won',function(){
alert("PROBANDO");
});
回答by Corinne Kubler
This should work, be sure you did not forget $(document).ready(function() {
这应该有效,确保你没有忘记 $(document).ready(function() {
$(document).ready(function() {
$('#won').change(function(){
alert( $(this).find("option:selected").attr('value') );
});
});