JQuery determines whether radio is selected and gets the selected value method summary
- 2020-05-30 19:22:34
- OfStack
Let me share with you
JQuery determines whether the radio marquee is selected and gets a value
https://www.ofstack.com/article/154840.htm
1. Judge the selection by obtaining the selected value
Go straight to the code and don't forget to reference the JQuery package
<!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">
/*------ judge radio Gets the selected value if it is selected --------*/
$(function(){
$("#btnSubmit").click(function(){
var val=$('input:radio[name="sex"]:checked').val();
if(val==null){
alert(" nothing !");
return false;
}
else{
alert(val);
}
var list= $('input:radio[name="list"]:checked').val();
if(list==null){
alert(" Please select the 1 a !");
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="10 Points satisfaction " />10 Points satisfaction
<input type="radio" name="list" value=" Satisfied with the " /> Satisfied with the
<input type="radio" name="list" value=" Not satisfied with " /> Not satisfied with
<input type="radio" name="list" value=" Very poor " /> Very poor
<br />
<input type="submit" value="submit" id="btnSubmit" />
</form>
</body>
</html>
radio cannot be judged by the equality of "checked", but only by true
<script type="text/javascript">
$(function () {
$("input").click(function () {
if ($(this).attr("checked")) {
alert(" selected ");
}
});
});
</script>
</head>
<body>
<input type="radio"/>
</body>
</html>
2. Use checked attribute to judge the selection
radio cannot be judged by the equality of "checked", only by true
<script type="text/javascript">
$(function () {
$("input").click(function () {
if ($(this).attr("checked")) {
alert(" selected ");
}
});
});
</script>
</head>
<body>
<input type="radio"/>
</body>
</html>
3. jquery gets the value of the radio radio button
$("input[name='items']:checked").val();
Also: determine if radio is selected and get the selected value
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. Get the values of 1 group of radio selected items
var item = $('input[name=items][checked]').val();
5. Set the radio button to be selected
$("input[type=radio]").attr("checked",'2');// Set up the value=2 Is the currently selected item