2 回答
TA贡献1799条经验 获得超8个赞
您可以使用$.each函数来获取inputs您拥有的所有内容,然后filter使用它们的属性将它们取出name。
input要通过二级密钥获取所有内容,[location]我们可以使用includes方法,这样我们将只获取containing输入key。
现场工作演示:
$('input').each(function(i, el) {
if ($(el).attr('name').includes('[location]')) {
console.log(el)
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="pref[1][term][]" value="no">
<input type="text" name="pref[1][term][]" value="no">
<input type="text" name="pref[1][location][]" value="get_this">
<input type="text" name="pref[1][location][]" value="get_this_too">
<input type="text" name="pref[1][other]" value="no">
<input type="text" name="pref[2][term][]" value="no">
<input type="text" name="pref[2][location][]" value="get_this_too">
<input type="text" name="pref[2][location][]" value="get_this_too">
<input type="text" name="pref[3][term][]" value="no">
<input type="text" name="pref[3][other]" value="no">
<input type="text" name="location" value="no">
TA贡献1966条经验 获得超4个赞
按照@AlwaysHelping 的建议使用filter:
$('input[name^="pref"]').filter('[name*="location"]')或者匹配结尾部分的名称属性,虽然有点老套。
$('input[name$="[location][]"]')添加回答
举报
