如何使用 jQuery 触发 select onchange 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15945267/
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
How to fire select onchange event with jQuery?
提问by csaron92
I have a dropdown list:
我有一个下拉列表:
<select size="1" name="filter"id="priority_filter" onchange="filter_user_trainings();">
<option value="all">All</option>
<option value="optional">Optional</option>
<option value="mandatory">Mandatory</option>
<option value="essential">Essential</option>
<option value="custom">Custom</option>
</select>
In a function
I call these:
在function
我称之为:
if(db==0 || db==1 ||db==2)
{
$("#priority_filter").val('custom');
}
I want to fire the select
onchange
function when the jQuery switches the value. How can I do this? The code above does not work.
我想select
onchange
在 jQuery 切换值时触发该函数。我怎样才能做到这一点?上面的代码不起作用。
回答by Adil
You can call change()
on select
to first or .trigger("change");
你可以叫change()
上select
以第一或.trigger("change");
if(db==0 || db==1 ||db==2)
{
$("#priority_filter").val('custom');
$("#priority_filter").change();
}
OR
或者
if(db==0 || db==1 ||db==2)
{
$("#priority_filter").val('custom').change();
}
回答by steo
$(document).ready(function(){
..code..
$('#priority_filter').on('change', function(){
..do your stuff..
}
..code..
});
回答by Dhaval Marthak
Try this: On load
试试这个:加载中
$("#priority_filter").change(function(){
var val = $("#priority_filter").val();
//alert(val);
//your code
});
Demo Here: http://jsfiddle.net/j8DPN/
演示在这里:http: //jsfiddle.net/j8DPN/