[英]How to get selected radio button value for multiple instances of radio button
[英]How to get last selected radio button id
我有兩個帶有一些單選按鈕的UL列表。 現在,我想從其UL
獲取最后選擇的單選按鈕ID attribute
。
例如:
UL
選擇了單選按鈕,則返回選擇的單選ID
。 UL
選擇了單選按鈕,則返回選擇的單選ID
。 我已經嘗試了下面的代碼,但是不能正常工作。 如果您嘗試從第一個UL
選擇第一個單選按鈕,則將收到具有選定ID
警報,但是當您從第二個UL
選擇單選按鈕時,您將得到第一個UL
警報,而我不想要。 :(
我想從該UL
獲取最后檢查的單選按鈕ID
。
任何想法如何做到這一點?
你能用這種方式檢查一下嗎:
UL
選擇20,您將獲得20。 UL
第二次選擇80,您將獲得80。 現在再次從第一個UL
更改單選按鈕,然后更改我想要的單選ID
。
這是我的JSFiddle 。
謝謝。
$("#update_cart").click(function() { var radioID = $("input[type='radio']:checked").attr("id"); if (radioID) { alert(radioID); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <ul class="one"> <li> <label for="1" class="label_check"> <input type="radio" id="1" name="one" value="10">10 </label> </li> <li> <label for="2" class="label_check"> <input type="radio" id="2" name="one" value="20">20 </label> </li> <li> <label for="3" class="label_check"> <input type="radio" id="3" name="one" value="30">30 </label> </li> <li> <label for="4" class="label_check"> <input type="radio" id="4" name="one" value="40">40 </label> </li> <li> <label for="5" class="label_check"> <input type="radio" id="5" name="one" value="50">50 </label> </li> </ul> <ul class="two"> <li> <label for="6" class="label_check"> <input type="radio" id="6" name="two" value="40">60 </label> </li> <li> <label for="7" class="label_check"> <input type="radio" id="7" name="two" value="70">70 </label> </li> <li> <label for="8" class="label_check"> <input type="radio" id="8" name="two" value="100">80 </label> </li> <li> <label for="9" class="label_check"> <input type="radio" id="9" name="two" value="120">90 </label> </li> <li> <label for="10" class="label_check"> <input type="radio" id="10" name="two" value="120">100 </label> </li> </ul> <input type="button" id="update_cart" class="update_cart" value="Update Cart">
采用
$("#update_cart").click(function () {
var radioID = $("input[type='radio'].active").attr("id");
if (radioID) {
alert(radioID);
}
});
$("input[type='radio']").click(function(){
$('.active').removeClass('active');
$(this).addClass('active');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.