具有相同名称但不同索引的 Jquery 循环复选框

我有一组复选框,它们都具有相同的名称但键不同:

<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 和定位包装器对象不是选项。有没有办法将它们选为一组并循环播放?


叮当猫咪
浏览 80回答 1
1回答

一只斗牛犬

您可以像这样选择和循环:$("input[name^='labels']").each(function(index) {&nbsp; 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(因为^=意思是“开头为”)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript