jquery Select and reverse click to perform a solution _jquery

Source: Internet
Author: User

Code requirements, using attr can only be performed once, using prop is perfect for full selection and inverse selection, get all the selected items and the text of the selected items into a string.

Solution One:

The code is as follows:

  
 

Summarize:

For the intrinsic properties of the HTML element itself, the prop method is used when processing.

For our own custom DOM attributes for HTML elements, the attr method is used when processing.

Reference http://www.jb51.net/article/62308.htm

Solution Two:

Problem Description:

$ (". Chooseall"). Click (function () {
 if ($ (". Chooseall"). attr ("checked") = = "Checked") {
$ ("Input[name=" CheckBox1 '] "). Removeattr (" Checked "," checked ");
 Console.log (1);
 } else{
 $ ("Input[name= ' CheckBox1 ')"). attr ("Checked", "checked");
 Console.log (2);
 }
});

The above code for the first click and second click, can achieve the full selection and the inverse of the function, but once again will no longer work, what is the situation?

Other than the first checkbox, the rest are dynamically generated by Ajax, does this have anything to do with it? Console.log Each click can alternately output 1 and 2, but the middle of the code can not be executed.

Solution:

Removeattr parameters only need one, removeattr ("checked")
However, it is recommended to replace

$ (". Chooseall"). Click (function () {
 if ($ (". Chooseall"). Prop ("checked") = = True) {
 $ (' input[name= ' CheckBox1 ') ]. Prop ("checked", false);
 Console.log (1);
 } else{
 $ ("Input[name= ' CheckBox1 ')"). Prop ("checked", false);
 Console.log (2);
 }
});

or more concise,

$ (". Chooseall"). Click (function () {
 var ischecked = $ (this). Prop ("checked");
 $ ("Input[name= ' CheckBox1 ']"). Prop ("Checked", ischecked);
});

The above is a full selection of jquery and reverse click to implement a solution, I hope to help.

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.