第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JQuery 調(diào)用 .elements 返回未定義

JQuery 調(diào)用 .elements 返回未定義

慕無忌1623718 2022-10-21 15:41:32
我正在嘗試使用 jQuery 遍歷一組復(fù)選框。復(fù)選框是用 HTML 編寫的,如下所示:<div id="checkboxes">     <div class="custom-control custom-checkbox">         <input type="checkbox" name="XXX" class="custom-control-input" id="a" value="XXX">         <label class="custom-control-label" for="a">XXX</label>     </div>     <div class="custom-control custom-checkbox">         <input type="checkbox" name="XXX" class="custom-control-input" id="b" value="XXX">         <label class="custom-control-label" for="b">XXX</label>     </div>     ...</div>我試圖用這個(gè)(對(duì)按鈕單擊的響應(yīng)的一部分)獲取 JQuery 中的復(fù)選框數(shù)組:var choices = document.getElementById("checkboxes").elements;但是,當(dāng)我調(diào)用 時(shí)for(let i = 0; i < choices.length; i++),控制臺(tái)中出現(xiàn)錯(cuò)誤:Uncaught TypeError: Cannot read property 'length' of undefined顯然,該變量choices未定義。我已經(jīng)做了一些研究,但我找不到問題出在哪里,或者我可以通過其他方式獲取復(fù)選框元素的數(shù)組。
查看完整描述

1 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

用于querySelectorAll()定位特定的元素集合

var choices = document.querySelectorAll('#checkboxes input[type="checkbox"]');


choices.forEach( el => console.log(el.id, el.value) )

<div id="checkboxes">

    <div class="custom-control custom-checkbox">

        <input type="checkbox" name="XXX" class="custom-control-input" id="a" value="XXX">

        <label class="custom-control-label" for="a">XXX</label>

    </div>

    <div class="custom-control custom-checkbox">

        <input type="checkbox" name="XXX" class="custom-control-input" id="b" value="XXX">

        <label class="custom-control-label" for="b">XXX</label>

    </div>

    

</div>



查看完整回答
反對(duì) 回復(fù) 2022-10-21
  • 1 回答
  • 0 關(guān)注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)