如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:05:20  来源:igfitidea点击:

How to fire select onchange event with jQuery?

jqueryhtml

提问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 functionI call these:

function我称之为:

if(db==0 || db==1 ||db==2)
{
   $("#priority_filter").val('custom');
}

I want to fire the selectonchangefunction when the jQuery switches the value. How can I do this? The code above does not work.

我想selectonchange在 jQuery 切换值时触发该函数。我怎样才能做到这一点?上面的代码不起作用。

回答by Adil

You can call change()on selectto 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/