javascript - Jquery Trigger checkbox: function tied to click event occurs before the checked attribute is set - Stack Overflow

I am implementing a "Billing address is same as Address" type function, which when a checkbox

I am implementing a "Billing address is same as Address" type function, which when a checkbox is checked fills the fields based on the other fields. Works perfectly.

The function for the click event..

    if ($(this).attr('checked')) {
        // copy address fields to billing fields
    }
    else{
        // clear fields
    }

Now I use an event (jquery hotkey plugin) to auto fill all of the fields in the form so I can easily and quickly demo and test the form. Instead of cheating and filling in the billing fields as the address fields i want to use

$("#CheckboxForAutofillId").trigger('click'); 

This does not work the first time I fire the event, as in the above function which is called it checks for the attribute of checked, however the trigger('click'), apparently calls the event BEFORE it changes the attribute.

Which means for me to simulate it correctly I have to do this..

$("#CheckboxForAutofillId").attr("checked", "checked"); // sets checked
$("#CheckboxForAutofillId").trigger('click');           // fires event then 'unchecks'
$("#CheckboxForAutofillId").attr("checked", "checked"); // rechecks

Reading the jquery page for trigger, I didn't notice this mentioned, and I haven't bothered to dig through the source to see what actually is occurring but it seems to me that ideally the trigger event should set the 'checked' attribute before firing the event itself as that is a better simulation of a person checking a checkbox?

Am I missing something?

I am implementing a "Billing address is same as Address" type function, which when a checkbox is checked fills the fields based on the other fields. Works perfectly.

The function for the click event..

    if ($(this).attr('checked')) {
        // copy address fields to billing fields
    }
    else{
        // clear fields
    }

Now I use an event (jquery hotkey plugin) to auto fill all of the fields in the form so I can easily and quickly demo and test the form. Instead of cheating and filling in the billing fields as the address fields i want to use

$("#CheckboxForAutofillId").trigger('click'); 

This does not work the first time I fire the event, as in the above function which is called it checks for the attribute of checked, however the trigger('click'), apparently calls the event BEFORE it changes the attribute.

Which means for me to simulate it correctly I have to do this..

$("#CheckboxForAutofillId").attr("checked", "checked"); // sets checked
$("#CheckboxForAutofillId").trigger('click');           // fires event then 'unchecks'
$("#CheckboxForAutofillId").attr("checked", "checked"); // rechecks

Reading the jquery page for trigger, I didn't notice this mentioned, and I haven't bothered to dig through the source to see what actually is occurring but it seems to me that ideally the trigger event should set the 'checked' attribute before firing the event itself as that is a better simulation of a person checking a checkbox?

Am I missing something?

Share Improve this question asked Jun 24, 2011 at 21:43 SteffanSteffan 3175 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

you should use change() instead of click().

$("#CheckboxForAutofillId").change(function()
{
    if(this.checked) ...
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742310618a4419819.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信