Home  >  Article  >  Web Front-end  >  JQuery determines whether the radio is selected and obtains the selected value sample code_jquery

JQuery determines whether the radio is selected and obtains the selected value sample code_jquery

WBOY
WBOYOriginal
2016-05-16 16:33:211097browse

Other radio operation functions will be added in the future. Go directly to the code, don’t forget to quote 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">
/*------判断radio是否有选中,获取选中的值--------*/
$(function(){
$("#btnSubmit").click(function(){
var val=$('input:radio[name="sex"]:checked').val();
if(val==null){
alert("什么也没选中!");
return false;
}
else{
alert(val);
}
var list= $('input:radio[name="list"]:checked').val();
if(list==null){
alert("请选中一个!");
return false;
}
else{
alert(list);
} 
});
});
</script>
</head>

<body>
<form id="form1" >
<input type="radio" name="sex" value="男" />男
<input type="radio" name="sex" value="女" />女
<br />
<input type="radio" name="list" value="十分满意" />十分满意
<input type="radio" name="list" value="满意" />满意
<input type="radio" name="list" value="不满意" />不满意
<input type="radio" name="list" value="非常差" />非常差
<br />
<input type="submit" value="submit" id="btnSubmit" />
</form>
</body>
</html>

Netizen comments: var boolCheck=$('input:radio[name="sex"]').is(":checked"); is better, this method is simpler

is() Detects a set of matching elements based on a selector, element, or jQuery object, returning true if at least one of these elements matches the given argument.

If the characters are garbled after you save, it is recommended to use notepad to process it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn