为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

叮当猫咪 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(因为^=意思是“开头为”)。



查看完整回答
反对 回复 2023-06-09
  • 1 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信