The single button and the check button are frequently used during development. Next I will use JQuery to operate the single button and check button:
Single button:
There are three ways to obtain a single-choice button object through JQuery:
① ID: $ ("# radioId ")
② NAME: $ (": input [name = 'radioname']")
③ TYPE: $ ("input [type = radio]"), which may be written on some materials: $ ("input [@ type = radio]" "), this is related to your JQuery version. If it is an old version, you can use the new version. If you do not know which version is suitable for use, you can try it one by one, it can be done in one attempt, and you can learn more. Why not!
We all know that only one of the single-choice buttons can take effect. To achieve this effect, many people may think that you can change the ID to the same one. In fact, after you try it, you will know that you can still select multiple radio buttons in a group. Because the key attribute to achieve this effect is not ID, but NAME.
After the introduction, let's put it into use:
After obtaining the single-choice button object, we have to traverse the object, because the object type is an array, and we need to judge whether it is selected one by one, then retrieve the required value from the selected button. The sample code is as follows:
Copy codeThe Code is as follows:
Var itemradio = $ ("input [type = radio]");
Result = "";
Nums = itemradio. length;
For (I = 0; I <nums; I ++ ){
If (itemradio [I]. checked ){
Result = itemradio [I]. value + "," + itemradio [I]. id + ";" + result;
}
}
The result is the value I want to obtain. you may understand the previous value, and some people in the latter ID may be confused. Here, I will make an extension: all objects in JS can directly obtain the attribute value through.
Check box:
For the check box, all we need is the effect of "select all" and "reselect". If JQuery is used, we can do it in just one sentence, as shown below:
Copy codeThe Code is as follows:
Var checkedObj = $ ('input: checkbox [name = "checkbox"]: checked ');
Var values = "";
CheckedObj. each (function (){
Var value = this. value + ",";
Values + = value;
});
Okay, you can use the above two sections of code to operate the check box and single-choice button!