Home > Web Front-end > JS Tutorial > body text

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

黄舟
Release: 2017-06-27 09:36:24
Original
5717 people have browsed it

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,...])
Copy after login

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");
});
Copy after login
$("#selectId").change(function(){
alert("选中的值为:"+$(this).val());
});
Copy after login

Give the select an id attribute

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

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

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template