亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JQuery 或 Javascript 從多維 HTML 名稱數組中選擇二級鍵

使用 JQuery 或 Javascript 從多維 HTML 名稱數組中選擇二級鍵

慕村9548890 2023-03-03 13:13:36
pref[*][location][]如何像使用 jQuery 選擇器或 Javascript一樣選擇所有具有名稱的輸入?我有興趣通過二級鍵“位置”獲取它。<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">
查看完整描述

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">


查看完整回答
反對 回復 2023-03-03
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

按照@AlwaysHelping 的建議使用filter

$('input[name^="pref"]').filter('[name*="location"]')

或者匹配結尾部分的名稱屬性,雖然有點老套。

$('input[name$="[location][]"]')


查看完整回答
反對 回復 2023-03-03
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號