简体   繁体   中英

how to get checkbox from multiple cells in html table?

I have dynamic html table and every cell have one checkbox. I want to get the selected checkbox if the user select from multiple checkbox from different row.

function GetAllChecked() {
             var chkedshid = new Array();
             var rows = new Array();
             rows = document.getElementById("Tbl_Id").getElementsByTagName("tr");
             trcount = rows.length;

             for (var i = 0; i < rows.length; i++) {
                 trid = rows[i].id;
                 chkedshid = chkedshid + chkedshid
                 if (inputList = document.getElementById(trid).getElementsByTagName("input")) {
                     for (var n = 0; n < inputList.length; n++) {
                         if (inputList[n].type == "checkbox") {
                             if (inputList[n].checked == true) {
                                 chkedshid[n] = inputList[n].id;

                             }
                         }
                     }
                 }
  }
             document.getElementById('Hidden_CellSelected').value = chkedshid.join();
             document.getElementById("BtnSav2Cart").click();
         }

why why this function return just last selected checkbox for last row in loop???? i need the all selected checkbox for all rows!!!!!!!

Assuming you are using jQuery.

Then you can simply do -

$("#myTableId input:checked")

If your checkbox have specific class then you can also do -

$("#myTableId .specificCheckboxClass:checked")

On some Button click try to execute this code

var checkedTransactions = $("input:[name=' idofcheckboxe ']:checked").map(function () { return $(this).val(); }).get();

var will return all id of check boxes which are selected

thanks all i solve the problem:

           function GetAllChecked() {
             var chkedshid = new Array();
             var rows = new Array();
             rows = document.getElementById("Tbl_Id").getElementsByTagName("tr");
             trcount = rows.length;

                  var totlchk = new Array();    
             for (var i = 0; i < rows.length; i++) {
                 trid = rows[i].id;


                 if (inputList = document.getElementById(trid).getElementsByTagName("input")) {
                     for (var n = 0; n < inputList.length; n++) {
                         if (inputList[n].type == "checkbox") {
                             if (inputList[n].checked == true) {
                                 chkedshid[n] = inputList[n].id;
                             }
                         }
                     }
                     totlchk = totlchk.concat(chkedshid.join());
                 }

             }

             document.getElementById('myHiddenfield').value = totlchk.join();
             document.getElementById("BtnSav2Cart").click();
         }
var table = document.getElementById("mytable");
checkbox = table.getElementsByTagName("input"); 

for(var indexCheckbox = 1; indexCheckbox<checkbox.length; indexCheckbox++){
    if(checkbox[indexCheckbox].checked){

        //do something
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM