mardi 30 juin 2015

Triggering "click" event on a checkbox or radio button toggles the "checked" property after the event handler function finish

Strange behave on jquery click event over radioButton and checkbox

when a user click on a radio button, the property check is toggle first and then the event handler is run.

when the code trigger click event on those elements, first the event handler is running and then the check property is changing.

example here: http://ift.tt/1IqqWAr

 <body >
  <input type="radio" id="test-checkbox" name="test" checked="true"/>
      <input type="radio" id="test-checkbox2" name="test"/>
  <span id="test-click">trigger click event</span>
 </body>

$(function(){


    $("#test-click").click(function(){

        $("#test-checkbox2").click();

    });

    $(document).on("click",'[name$="test"]',function () { 

        print();

    });
    var print=function(){
    alert( $('input[name$="test"]:checked').attr("id"));

    };
});

how can I avoid this problem and trigger the click after the property check is toggle?




Aucun commentaire:

Enregistrer un commentaire