使用jQuery获取单选按钮radio的选中值

时间:2023-01-23 19:34:29

简单来说:

var val = $('input[name="sex"]:checked').val();

整体代码:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>获取单选按钮选中的值</title>
</head>
<body>
<label><input type="radio" name="sex" value="m" checked="checked"></label>
<label><input type="radio" name="sex" value="f"></label>
<button>提交</button>
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('button').click(function(){
var val = $('input[name="sex"]:checked').val();
console.log(val);
});
})
</script>
</body>
</body>
</html>