在Chrome中选择jquery-1.9.1会触发两次更改事件

Elc*_*_91 4 jquery

我似乎无法弄清楚为什么更改事件会为此select元素触发两次:

<form name="contactform">
  <label for="requesttype">Request Type:</label>            
  <select name="requesttype" class="reqtype">
     <option value="1" selected>General Comment / Request</option>
     <option value="2">No Cost Services Quote</option>
  </select>
</form>
Run Code Online (Sandbox Code Playgroud)

使用此jquery代码时:

$(function() {
    $(".reqtype").change(function(){
        alert($(".reqtype option:selected").val());
    })
});
Run Code Online (Sandbox Code Playgroud)

我仔细检查了我使用"reqtype"类的唯一地方是在select元素中.任何帮助,将不胜感激.

小智 9

有类似的问题,并尝试了我能找到的所有解决方案.对我来说唯一有效的是在变更检查之前解除绑定(上面的DevPat提到):

jQuery("#wfselect").unbind(); 
jQuery("#wfselect").change(function() { getWFDetail(); });
Run Code Online (Sandbox Code Playgroud)


bp4*_*p4D 2

在这种情况下,在绑定事件处理程序之前使用 .unbind() 会产生干净的 JS。你可以参考jQuery文档