JQuery judges whether radio (single-choice) selects and obtains the selected value method summary, jqueryradio

Source: Internet
Author: User

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

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.