操作方法
同一组radio拥有相同的name值,使用 :checked
选择器,即可获取到选择的radio的value值,例如:
$("input[name='xxx']:checked").val()
示例HTML代码
</div>
<div><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery 获取Radio选中值</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<h4>Please select your gender.</h4>
<p>
<label><input type="radio" name="xxx" value="male">男</label>
<label><input type="radio" name="xxx" value="female">女</label>
</p>
<p><input type="button" value="获取"></p>
</body>
<script type="text/javascript">
$(document).ready(function(){
$("input[type='button']").click(function(){
var radioValue = $("input[name='xxx']:checked").val();
if(radioValue){
alert("Your are a - " + radioValue);
}
});
});
</script>
</html></div>
<div>
转载请注明:悠然品鉴 » jQuery获取radio单选的值