How does jQuery determine and set whether multiple choice is selected ??

Source: Internet
Author: User

How does jQuery determine and set whether multiple choice is selected ??

How can I determine whether multiple choice is selected in HTML?

This is the most common judgment in web programming, but some time ago, it encountered a very strange problem.

I think many people will judge this way.

If ($ ("# weixuanzhong"). attr ("checked") = "checked") // when selected {}

However, for the following HTML code:

 

It is found that the above judgment is always true, no matter whether multiple selection boxes are selected or not. So I was surprised that I used to make such a decision. I didn't seem to have found any problems.


So we found that this property is actually different under different circumstances.

1 When the checked = "checked" attribute is not in HTML

For example:

HTML code

 

This can be done to determine whether to select:

If ($ ("# xuanzhong "). prop ("checked") // when selected, it is true {alert ($ ("# xuanzhong "). prop ("checked "));}


When there is a checked = "checked" attribute in 2HTML, if ($ ("# weixuanzhong") cannot be used "). attr ("checked") = "checked") to determine whether or not to select,

HTML code

This can be done to determine whether to select: 


If ($ ("# xuanzhong "). attr ("checked") = "checked") // whether selected or not, it is true and cannot be judged {alert ($ ("# xuanzhong "). attr ("checked");} if ($ ("# xuanzhong "). prop ("checked") // when selected, it is true {alert ($ ("# xuanzhong "). prop ("checked "));}

3. How do I select multiple options?

In general, we use checked = "checked" to set the selected item. It seems that there is no problem, but if it is the following HTML code,

Select multiple by default
The following code does not ensure that multiple selections are selected. Even if you set them, multiple selections are not selected.

 $("#xuanzhong").attr("checked", "checked");


If there is no checked = "checked" attribute, the HTML code is as follows:

You can use the following script to set the selected status. 

 $("#xuanzhong").attr("checked", "checked");


In this case, the selected status of the multiple selection box is set. If $ ("# xuanzhong") is used "). attr ("checked", "checked"); in this way, you must ensure that there is no checked = "checked" in the original HTML. Otherwise, an error occurs and the expected effect cannot be achieved.

However, you can select $ ("# xuanzhong"). prop ("checked", true) in any case.



After testing, we found that we were able to pass $ ("# xuanzhong "). attr ("checked") = "checked" determines whether multiple selection boxes are selected. It is precisely because there is no checked = "checked" in HTML ", each time you use $ ("# xuanzhong "). attr ("checked", "checked"); this method sets the selected status, so there is no error.



Therefore, determine whether to use $ ("# xuanzhong") in the multiple selection box "). prop ("checked") instead of $ ("# xuanzhong "). attr ("checked") = "checked", set to use $ ("# xuanzhong "). prop ("checked", true), instead of $ ("# xuanzhong "). attr ("checked", "checked"); otherwise, an error occurs if you do not pay attention to it.


After testing, it is found that the single-choice and multi-choice are the same, and there is no checked = "checked" in HTML.

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.