I have a grid to which I can dynamically add rows and columns. Each column has a checkbox next to it and checking it makes all checkboxes for that column be selected.

我有一个网格,我可以动态添加行和列。每列旁边都有一个复选框,选中它会使该列的所有复选框都被选中。

It looks like this:

它看起来像这样:

Below is my code to add columns by clicking the "Add another column" button.

下面是我通过单击“添加其他列”按钮添加列的代码。

$('#btnAddCol').click(function () {
    myfrom.find('tr').each(function(){
        var trow = $(this);
        var columnName = $('#colorName').val()
        if(trow.index() === 1){
          trow.append('<th > ' + columnName + ' <input type="checkbox" class="colmaster"/>');
        }else if (trow.index() > 1){
            trow.append('<td><input type="checkbox" name="'+columnName+'" class="child" /></td>');
        }
    });
    iter += 1;
});

And here is the code for selecting all checkboxes in the column.

这是用于选择列中所有复选框的代码。

$('.colmaster').click(function(){
 var idx = $(this).parent().index();
 $('#colorgrid td:nth-child(' + (idx + 1) + ') input.child').prop('checked', this.checked);
})

Question For the newly added columns, justadded in the screenshot above, when I check the checkbox it doesn't seem to select all checkboxes in that column

问题对于新添加的列,刚刚在上面的屏幕截图中添加,当我选中复选框时,它似乎没有选中该列中的所有复选框

Here is a working fiddle for everything: http://jsfiddle.net/y55nn1qx/4/

这是一切工作的小提琴:http://jsfiddle.net/y55nn1qx/4/

2 个解决方案

#1


3

$('.colmaster').click will only bind the event to .colmaster elements that exist at the time of running the code. Try something like this:

$('。colmaster')。click只会将事件绑定到运行代码时存在的.colmaster元素。尝试这样的事情:

$('#colorgrid').on('click', '.colmaster', function() {
     // insert your .colmaster.click code here
});

Here's the jQuery API reference for on.

这是关于on的jQuery API参考。

更多相关文章

  1. 如何在JavaScript中编写适当的OOP代码?
  2. 高性能JavaScript代码高亮插件
  3. js“DOM事件”之鼠标事件、js的测试方法、js代码的放置位置
  4. JavaScript 最佳实践:帮你提升代码质量
  5. 使用javascript访问网格内的文本框
  6. Angularjs指令ng-hide在单元格模板中不对网格数据的更改进行绑定
  7. 利用javascript实现遍历xml文件的代码实例
  8. javascript 的MD5代码备份,跟java互通
  9. 这些年,我收集的JavaScript代码(二)

随机推荐

  1. 将mysql查询结果转换为CSV(带有复制/粘贴
  2. 使用MySQL正则表达式 __MySQL必知必会
  3. Mysql存储过程创建失败,版本5.5,请高手解决
  4. MySql日志与事务的隔离级别
  5. 在Win2003+Tomcat+MySQL下运行JPetStore
  6. 【20110115 InfoQ 百度技术沙龙 2010 第
  7. 【转】mysql-innodb-buffer pool 结构分
  8. C++编写数据库备份程序,支持MSSQL\Oracle
  9. 保持最新的一个字段值,直到它发生变化,然后
  10. mysql-proxy主从搭建读写分离全过程