Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery-1.9.1 fires change event twice on select in Chrome

Tags:

jquery

I can't seem to figure out why the change event fires twice for this select element:

<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>

When using this jquery code:

$(function() {
    $(".reqtype").change(function(){
        alert($(".reqtype option:selected").val());
    })
});

I double-checked that the only place I am using the class "reqtype" is in the select element. Any help would be appreciated.

like image 475
Elcid_91 Avatar asked Oct 05 '13 12:10

Elcid_91


2 Answers

Had a similar problem and tried all the solutions I could find. The only one that worked for me was unbind before the change check (mentioned by DevPat above):

jQuery("#wfselect").unbind(); 
jQuery("#wfselect").change(function() { getWFDetail(); });
like image 181
BuckRogers Avatar answered Oct 17 '22 01:10

BuckRogers


In situations like this using .unbind() before binding the event handler results in a clean JS. You can refer to jQuery documentation

like image 34
bp4D Avatar answered Oct 17 '22 02:10

bp4D