[英]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.