<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div> <input type="radio" name="gender" value="male"> Male <input type="radio" name="gender" value="female"> Female <input type="radio" name="gender" value="other"> Other </div> <button id="btn">Get Value</button> $(document).ready(function() { $("#btn").click(function() { var selectedValue = $("input[name='gender']:checked").val(); console.log(selectedValue); }); }); </body> </html>
时间: 2023-07-19 19:15:28 浏览: 77
你的代码有问题,jQuery代码部分应该放在`<script></script>`标签中。修改后的代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<input type="radio" name="gender" value="other"> Other
</div>
<button id="btn">Get Value</button>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#btn").click(function() {
var selectedValue = $("input[name='gender']:checked").val();
console.log(selectedValue);
});
});
</script>
</body>
</html>
```
在这个例子中,我使用了CDN引入了jQuery库,你可以根据自己的需求选择是否使用。
阅读全文