如何使用jQuery触发select onchange事件?

时间:2021-05-01 20:34:04

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:

在函数中我称之为:

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.

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

3 个解决方案

#1


20  

You can call change() on select to first or .trigger("change");

您可以在select to first或.trigger(“change”)上调用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();      
}

#2


3  

$(document).ready(function(){

 ..code..
$('#priority_filter').on('change', function(){
     ..do your stuff..
 } 
..code..

});

#3


0  

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/

#1


20  

You can call change() on select to first or .trigger("change");

您可以在select to first或.trigger(“change”)上调用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();      
}

#2


3  

$(document).ready(function(){

 ..code..
$('#priority_filter').on('change', function(){
     ..do your stuff..
 } 
..code..

});

#3


0  

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/