JQuery judges whether radio (single-choice) selects and obtains the selected value method summary, jqueryradio
1. Use the obtained selected value to determine the selected value
Directly upload the code. Do not forget to reference the JQuery package.
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> JQuery radio </title>
<Script type = "text/javascript" language = "javascript" src = "JavaScript/jquery-1.6.1.min.js"> </script>
<Script type = "text/javascript" language = "javascript">
/* ------ Determine whether radio is selected and obtain the selected value --------*/
$ (Function (){
$ ("# BtnSubmit"). click (function (){
Var val = $ ('input: radio [name = "sex"]: checked'). val ();
If (val = null ){
Alert ("Nothing selected! ");
Return false;
}
Else {
Alert (val );
}
Var list = $ ('input: radio [name = "list"]: checked'). val ();
If (list = null ){
Alert ("Please select one! ");
Return false;
}
Else {
Alert (list );
}
});
});
</Script>
</Head>
<Body>
<Form id = "form1">
<Input type = "radio" name = "sex" value = "male"/> male
<Input type = "radio" name = "sex" value = "female"/> female
<Br/>
<Input type = "radio" name = "list" value = "very satisfied"/> very satisfied
<Input type = "radio" name = "list" value = "satisfied"/> satisfied
<Input type = "radio" name = "list" value = "not satisfied"/> not satisfied
<Input type = "radio" name = "list" value = "very poor"/> very poor
<Br/>
<Input type = "submit" value = "submit" id = "btnSubmit"/>
</Form>
</Body>
</Html>
Radio cannot be determined by equal "checked", but only by true
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("Input"). click (function (){
If ($ (this). attr ("checked ")){
Alert ("selected ");
}
});
});
</Script>
</Head>
<Body>
<Input type = "radio"/>
</Body>
</Html>
Ii. Use the checked attribute to determine the selected
Radio cannot be determined by equal "checked", but only by true
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("Input"). click (function (){
If ($ (this). attr ("checked ")){
Alert ("selected ");
}
});
});
</Script>
</Head>
<Body>
<Input type = "radio"/>
</Body>
</Html>
3. jquery obtains the value of the radio button.
Copy codeThe Code is as follows:
$ ("Input [name = 'items ']: checked"). val ();
In addition, determine whether radio is selected and obtain the selected value.
As follows:
Copy codeThe Code is as follows:
Function checkradio (){
Var item = $ (": radio: checked ");
Var len = item. length;
If (len> 0 ){
Alert ("yes -- the selected value is:" + $ (": radio: checked"). val ());
}
}
4. Obtain the value of a set of radio selected items
Copy codeThe Code is as follows:
Var item = $ ('input [name = items] [checked] '). val ();
5. Set the radio button to be selected
Copy codeThe Code is as follows:
$ ("Input [type = radio]"). attr ("checked", '2'); // set the project with value = 2 to the selected one