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][]"]')
添加回答
舉報