Determine if radio has a check to get the selected value

Source: Internet
Author: User

article source: http://www.jb51.net/article/64141.htm

first, use to get the selected value to determine the selected

Directly on the code, don't forget to refer to the jquery package
Copy code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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 if Radio is selected, get the selected value--------* *
$ (function () {
$ ("#btnSubmit"). Click (function () {
var val=$ (' Input:radio[name= "Sex"]:checked "). Val ();
if (val==null) {
Alert ("Nothing is 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>

<body>
<form id= "Form1" >
<input type= "Radio" name= "Sex" value= "male"/> Male
<input type= "Radio" name= "Sex" value= "female"/>
<br/>
<input type= "Radio" name= "list" value= "very satisfied"/> Very satisfied
<input type= "Radio" name= "list" value= "Satisfaction"/> Satisfaction
<input type= "Radio" name= "list" value= "dissatisfied"/> Dissatisfied
<input type= "Radio" name= "list" value= "very poor"/> very poor
<br/>
<input type= "Submit" value= "Submit" id= "Btnsubmit"/>
</form>
</body>

Radio cannot be judged by "checked" equality, only by using true
Copy code code as follows:
<script type= "Text/javascript" >
$ (function () {
$ ("Input"). Click (function () {
if ($ (this). attr ("checked")) {
Alert ("Checked");
}
});
});
</script>
<body>
<input type= "Radio"/>
</body>

second, use the checked attribute to judge the selected

Radio cannot be judged by "checked" equality, only by using true
Copy code code as follows:
<script type= "Text/javascript" >
$ (function () {
$ ("Input"). Click (function () {
if ($ (this). attr ("checked")) {
Alert ("Checked");
}
});
});
</script>
<body>
<input type= "Radio"/>
</body>

jquery Gets the value of the Radio radio button
Copy code code as follows:
$ ("Input[name= ' Items ']:checked"). Val ();

Another: Determine if Radio is selected and gets the selected value

As shown below:
Copy code code as follows:
function Checkradio () {
var item = $ (": radio:checked");
var len=item.length;
if (len>0) {
Alert ("yes--selected value is:" +$ (": radio:checked"). Val ());
}
}

get the value of a set of radio selected items
Copy code code as follows:
var item = $ (' input[name=items][checked] '). Val ();

Five, the Set radio button is selected

Copy code code as follows:
$ ("Input[type=radio]"). attr ("Checked", ' 2 ');//Set value=2 item as current selected

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.