[英]Jquery get value of the current selected radio button
我有以下单选按钮:
<label>Referred to Health Facility</label>
<input required name="Health_Ref" type="radio" class="wellness_center radiostyle Health_Ref" id="Health_Ref" value="Yes">
<div class="radiolabel">Yes</div>
<input name="Health_Ref" type="radio" required class="wellness_center radiostyle Health_Ref" id="Health_Ref" value="No" checked="checked">
<div class="radiolabel">No</div>
下面是我的jquery函数来获取值:
$(document).ready(function () {
$(".Health_Ref").click(function () {
var value = $(".Health_Ref").val();
alert(value);
});
});
当我单击第一个或第二个按钮时返回的值是第一个按钮,是的,实现jquery函数的最佳方法是什么,这样我才能获得单击的单选按钮的值?
您可能想要尝试将:checked
添加到您的jquery选择器中。 :checked的文档
:checked
$(document).ready(function () {
$(".Health_Ref").click(function () {
var value = $(".Health_Ref:checked").val();
alert(value);
});
});
$(this)
我使代码更简单。 this
是事件处理程序中元素的值,因此您可以简单地使用$(this)
将元素变成jQuery对象。
$(document).ready(function () {
$(".Health_Ref").click(function () {
var value = $(this).val();
alert(value);
});
});
密码笔: http : //codepen.io/anon/pen/wKgBGN
var value = $(“。Health_Ref:checked”)。val();
这仅选择当前选中的单选按钮。
复选框也一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.