叮當貓咪
2023-06-09 17:47:42
我有一組復選框,它們都具有相同的名稱但鍵不同:<input type="checkbox" id="label-featured" name="labels[featured]" value="1">
<input type="checkbox" id="label-new" name="labels[new]" value="1">
<input type="checkbox" id="label-old" name="labels[old]" value="1">
<input type="checkbox" id="label-promo" name="labels[promo]" value="1">更改 html 和定位包裝器對象不是選項。有沒有辦法將它們選為一組并循環播放?
1 回答

一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
您可以像這樣選擇和循環:
$("input[name^='labels']").each(function(index) {
console.log(index + ": " + $(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="label-featured" name="labels[featured]" value="1">
<input type="checkbox" id="label-new" name="labels[new]" value="1">
<input type="checkbox" id="label-old" name="labels[old]" value="1">
<input type="checkbox" id="label-promo" name="labels[promo]" value="1">
選擇器"input[name^='labels']"
將抓取所有<input>
名稱以開頭的元素labels
(因為^=
意思是“開頭為”)。
添加回答
舉報
0/150
提交
取消