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

csa*_*n92 7 html jquery

我有一个下拉列表:

<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>
Run Code Online (Sandbox Code Playgroud)

function我称之为:

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

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

Adi*_*dil 24

你可以叫change()select以第一或.trigger("change");

if(db==0 || db==1 ||db==2)
{
   $("#priority_filter").val('custom');    
   $("#priority_filter").change();    
}
Run Code Online (Sandbox Code Playgroud)

要么

if(db==0 || db==1 ||db==2)
{
   $("#priority_filter").val('custom').change();      
}
Run Code Online (Sandbox Code Playgroud)