Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

html - how to get the ids of checked checkboxes in jquery

I want to get the ids of checked checkboxes and store those ids in an array using jquery. Can anybody give me correct code for it.

I have tried

Thanks in advance :)

$("#merge_button").click(function(event){
    event.preventDefault();
    var searchIDs = $("#find-table input:checkbox:checked").map(function(){
        return $(this).val();
    }).toArray();
    console.log(searchIDs);
});
question from:https://stackoverflow.com/questions/65858003/how-to-get-the-ids-of-checked-checkboxes-in-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the attr() method to get id. Like this:

$(this).attr('id');

Do you need such a result?

$("#merge_button").click(function(event){
    event.preventDefault();
    var searchIDs = $("#find-table input:checkbox:checked").map(function(){
        return $(this).attr('id');
    }).toArray();
    console.log(searchIDs);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="find-table">
  <input id="1" type="checkbox">
  <input id="2" type="checkbox">
  <input id="3" type="checkbox">
</div>

<button id="merge_button">merge</button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...