[英]Radio button group validation of dynamically generated name fields
碼:
html
<form id="myform" type="post">
<fieldset id="myid1">
<input id="entries_8490_burn_id_1" name="entries[8490][burn_id]" value="1" type="radio"/>
<input id="entries_8490_burn_id_2" name="entries[8490][burn_id]" value="2" type="radio"/>
<input id="entries_8490_burn_id_3" name="entries[8490][burn_id]" value="3" type="radio"/>
</fieldset>
<fieldset id="myid2">
<input id="entries_8491_burn_id_1" name="entries[8491][burn_id]" value="1" type="radio"/>
<input id="entries_8491_burn_id_2" name="entries[8491][burn_id]" value="2" type="radio"/>
<input id="entries_8491_burn_id_3" name="entries[8491][burn_id]" value="3" type="radio"/>
</fieldset>
<input type="submit" />
</form>
JS / jQuery
$( document ).ready(function() {
$('#myform').on('submit', function(e) {
e.preventDefault();
$("fieldset[id^='myid']").each(function () {
myid = this.id
alert(myid)
alert($(myid +" > input[name^='entries']").is(":checked"));
});
});
});
小提琴: http : //jsfiddle.net/woav90mz
問題:
當我檢查其中一個組中的單選按鈕時,我仍然會收到“ false false”。 不知何故沒有正確選擇組! 誰能發現我的錯誤?
您缺少#
-> 更新小提琴
alert($("#" + myid + " > input[name^='entries']").is(":checked"));
而且,如果您只是想確保每個組都選中了一個單選按鈕,則可以執行以下操作:
$("fieldset[id^='myid']").find(" > input[name^='entries']:checked").length;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.