我在表單上動態拖拽和顯示單選按鈕問題(由jquery選項卡分成不同的部分)。檢查每個組中至少選擇一個單選按鈕(使用jquery)
所以有很多2,3,4,5等單選按鈕放在一起,它們具有相同的「名稱」但不同的ID。
當您在各個部分之間點擊下一個時,我想檢查是否爲每個組選擇了至少一個單選按鈕。
(加載窗體時,單選按鈕都沒有默認選中的,它必須是這樣。)
我可以遍歷當前部分的單選按鈕,但我不知道如何輕鬆地檢查那一個已經被檢查過每個組?
我在表單上動態拖拽和顯示單選按鈕問題(由jquery選項卡分成不同的部分)。檢查每個組中至少選擇一個單選按鈕(使用jquery)
所以有很多2,3,4,5等單選按鈕放在一起,它們具有相同的「名稱」但不同的ID。
當您在各個部分之間點擊下一個時,我想檢查是否爲每個組選擇了至少一個單選按鈕。
(加載窗體時,單選按鈕都沒有默認選中的,它必須是這樣。)
我可以遍歷當前部分的單選按鈕,但我不知道如何輕鬆地檢查那一個已經被檢查過每個組?
在沒有元素之間的任何關係或者如何找到組名的情況下,我只能給你一個指針,但是這個應該可以工作(點擊id="nextButton"
的元素觸發檢查,但顯然這可以是定製以任何其他合適的事件,如$('form').submit()
,例如):
$(document).ready(
function(){
$('#nextButton').click(
function(){
var radioName = something; // use this if there's a relationship between the element
// triggering this check and the radio buttons
if ($('input[name='+ radioName +']:checked').length) {
// at least one of the radio buttons was checked
return true; // allow whatever action would normally happen to continue
}
else {
// no radio button was checked
return false; // stop whatever action would normally happen
}
}
);
}
);
$("input[@name='the_name']:checked").length;
if ($("input:checked").length < Num_of_groups)
{
alert ("Please select atleast one radio button in each group");
return false;
}
嘗試此
if ($('input[name='+radioName+']:checked').length == '0'){
// do something
return false;
}
這與接受的答案基本相同,只是更糟。你爲什麼要比較'數字'字段和'字符串'字面值? – meskobalazs 2015-02-16 10:53:20
自從jQuery 1.1.4(在評論部分)](http://api.jquery.com/attribute-equals-selector/),'@'語法已被棄用。 – 2010-10-26 22:57:50