mardi 23 décembre 2014

Changing Checkbox state before firing click manually

I am using an old version of JQuery, and I need my Checkboxes to change their checked state before executing their onclick method when click is triggered programatically.


HTML:



<input type="checkbox" id="cb_1" name="cb_1" onclick="Write(this)" />
<input type="text" id="tb_1" name="tb_1" />
<br />
<input type="checkbox" id="cb_2" name="cb_2" onclick="Write(this)" />
<input type="text" id="tb_2" name="tb_2" />
<br />
<input type="checkbox" id="cb_3" name="cb_3" onclick="Write(this)" />
<input type="text" id="tb_3" name="tb_3" />
<br />
<input type="button" id="bt" name="bt" value="Write All Checkboxes" onclick="WriteAll()" />


JS:



function Write($this)
{
var _Selected = $($this).is(':checked');
$('#' + $this.id.replace('c', 't')).val(_Selected);
}

function WriteAll()
{
$('[id^="cb_"]').each(function()
{
if(!$(this).is(':checked'))
{
$(this).trigger('click');
}
});
}


As explained in this ticket,



When a user click on a Checkbox with a click listener, it first flips the checked state and then fires the event. When a developer initiates a click on a Checkbox with jQuery, it first fires the event and then flips the checked state.



This kind of behaviour is fixed on JQuery > 1.8.3, see:


Jquery 1.9.1


JQuery 1.8.3


But it is not an option to change JQuery version for me, hence does someone have any idea?





Aucun commentaire:

Enregistrer un commentaire