iT邦幫忙

0

知道id 但不知道怎麼取得元素

  • 分享至 

  • xImage

試過把'#id'變成字串組起來但還是沒辦法

function inordering(){
    var lod=[];
    var checkboxes = document.querySelectorAll('input[type=checkbox]:checked');
    for (var i = 0; i < checkboxes.length; i++) {
        gg=checkboxes[i].id
        console.log()        //$('#id')
    }
}
huahualiu iT邦新手 2 級 ‧ 2021-12-15 23:48:17 檢舉
看不懂你的問題
錯誤訊息是啥?
你期待有啥結果?
qpalzm iT邦新手 1 級 ‧ 2021-12-16 08:17:31 檢舉
let id =$('input[type=checkbox]:checked').attr('id');
不知道是不是你要的
greenriver iT邦研究生 5 級 ‧ 2021-12-16 08:38:47 檢舉
可能是要抓取被選取的checkbox的id
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
海綿寶寶
iT邦大神 1 級 ‧ 2021-12-16 09:19:57

https://ithelp.ithome.com.tw/upload/images/20211216/20001787hudyQy0OyL.png

如果能提供貼好程式碼jsfiddle.net的連結尤佳

1
japhenchen
iT邦超人 1 級 ‧ 2021-12-16 09:20:48
document.querySelectorAll('input[type="checkbox"]:checked');
0
greenriver
iT邦研究生 5 級 ‧ 2021-12-16 09:53:46

顯示被選取的checkbox的id

<html>
    <body>
        <input type="checkbox" class="myCheckbox" id="myCheck1">
        <input type="checkbox" class="myCheckbox" id="myCheck2">
        <input type="checkbox" class="myCheckbox" id="myCheck3">
        <input type="checkbox" class="myCheckbox" id="myCheck4">
        <input type="checkbox" class="myCheckbox" id="myCheck5">
        <button onclick="showCheckedID()">show Checked Checkbox ID</button>
        
        <script>
            var array = []
            var myCheckbox = document.querySelectorAll(".myCheckbox")
            function showCheckedID() {
                array=[]
                for (var i = 0; i < myCheckbox.length; i++) { 
                  if(myCheckbox[i].checked){
                    array.push(myCheckbox[i].getAttribute('id'))
                  }
                }
                console.log(array)
            }
        </script>
    </body>
</html>

我要發表回答

立即登入回答