Home >Web Front-end >JS Tutorial >Detailed explanation of how to trigger select's change event code in jquery

Detailed explanation of how to trigger select's change event code in jquery

黄舟
黄舟Original
2017-06-27 09:36:245744browse

You can use jQuery's trigger() method to respond to events

Definition and usage

The trigger() method triggers the specified event type of the selected element.

Syntax

  $(selector).trigger(event,[param1,param2,...])

Parameter description

event Required. Specifies the event to be triggered by the specified element. You can make custom events (attached using bind() function), or any standard event.

[param1,param2,...] Optional. Additional parameters passed to the event handler procedure. The extra parameters are especially useful for custom events.

Example:

Trigger the change event of the select element:

$("button").click(function(){
  $("select").trigger("change");
});
$("#selectId").change(function(){
alert("选中的值为:"+$(this).val());
});

Give the select an id attribute

<select id="sel"></select>
$("#sel").bind("onchange",function(){
//自己写代码,这个就是给select空间添加onchange事件
});
function changeProtocol() { 
$(&#39;#step4 #protocol&#39;).empty(); 
$(&#39;#step4 # protocol&#39;).append("<option value=&#39;"+key+"&#39;>"+value+"</option>"); 
}

Clear the select first, Then reset the option. After this process is completed, how to trigger the change event?

$("#step #protocol").change();
$(&#39;#step4 #protocol&#39;).trigger(&#39;change&#39;)
$(&#39;#step4 #  protocol &#39;).bind(&#39;change&#39;,function(){});

The above is the detailed content of Detailed explanation of how to trigger select's change event code in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn