English 中文(简体)
如何从 html 表格中的多个单元格获得复选框?
原标题:how to get checkbox from multiple cells in html table?

我有动态 html 表格, 每个单元格都有一个复选框。 如果用户从不同行的多个复选框中选择多个复选框, 我想获得选中的复选框 。

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

最佳回答

感谢大家,我解决了问题:

           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();
         }
问题回答

假设你用的是jQuery

那样你就可以...

$("#myTableId input:checked")

如果您的复选框有特定的类,您也可以-

$("#myTableId .specificCheckboxClass:checked")

在某些按键点击中尝试执行此代码时

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

var 会返回选中的 复选框的全部 ID

var table = document.getElementById("mytable");
checkbox = table.getElementsByTagName("input"); 

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

        //do something
    }   
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签