Problems encountered when triggering the checkbox Click Event

Source: Internet
Author: User

For some reason, you need to implement one-choice checkbox. The method is to use jquery to select all the selected checkboxes except the selected checkbox, then, the click event is triggered (because the status of each checkbox must be changed to false, so the checked status cannot be changed to false), but it enters an infinite loop inexplicably.

 
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <HTML>  

When cbx is selected and cbx2 is clicked, the first click function bound to cbx2 is executed first, and its output is true. This indicates that the checkbox status is reversed when you click, then, the event function is triggered. Therefore, in the second bound click function, the IF condition is true and the following code is executed:

 
$ (': Checkbox: checked'). Not (this). Click ();

This sentenceCodeSelect the selected checkbox except the checkbox you just clicked. In this example, only cbx is used to trigger the click event, if the event function is triggered after the status is reversed as mentioned above, the execution process should be:

Execute the first click function bound to cbx and output false;

Run the second click function bound to cbx. If conditions are false, the execution is complete.

But the actual situation is an infinite loop, which always triggers the click functions of the two checkboxes. if you do not reverse the status before triggering the event function, the IF condition in the click function of cbx2 for the first time will be false, and will not cause a loop. after studying for half a day, I finally figured out the situation:

If you click with the mouse or press a space when the focus is obtained, the click event is triggered after its status is reversed;

If jquery uses code to trigger its Click Event, the click event is triggered first and then the status is reversed.

Therefore, if you want to implement the functions mentioned at the beginning, you only need to manually change the checked status to false, and then trigger the click event. After the click event is triggered, the status changes again, changed from false to true. Therefore, you must manually change the checked status again:

$ (': checkbox: checked '). not (this ). ATTR ('checked', false ). click (). ATTR ('checked', false); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.