Javascript - >根据转发器的复选框(ParentCheckbox)选中/取消选中gridview的复选框(ChildCheckbox)

时间:2012-03-22 13:38:33

标签: javascript jquery asp.net

我下面有一个javascript函数 - 实际上我在转发器中有一个复选框,里面有一个gridview,它还有一些复选框。

我想根据转发器复选框选择启用禁用gridviewCheckboxes。

所以我在itemsRepeater_ItemDataBound上注册了一个javacript。捕获 - 转发器的哪个复选框 - > (RepeaterCheckBoxId)和GridView(Grid)如下所示。

function EnableDisable(RepeaterCheckboxid,Grid)   {

     var grid = document.getElementById(Grid);
     if (grid == null)
         return;

     //variable to contain the cell of the grid
     var cell;

     if (grid.rows.length > 0) {
         //loop starts from 1. rows[0] points to the header.
         for (i = 1; i < grid.rows.length; i++) {
             //get the reference of first column
             cell = grid.rows[i].cells[1];

             cell.childNodes[0].checked = document.getElementById(RepeaterCheckboxid).checked;//If repeater checkbox is Unchecked then gridview checkboxes should be unchecked.* This is not working.

             cell.childNodes[0].disabled = !document.getElementById(RepeaterCheckboxid).checked; //If repeater checkbox is checked then gridViewCheckboxes are enabled and vice versa.This is working.


         }
     }
 }

但我观察到启用禁用工作正常,但检查未选中无效。 如果未选中转发器复选框,如何取消选中gridview中的所有复选框?

如果你们有任何想法,请帮帮我。

3 个答案:

答案 0 :(得分:0)

试试这样:

cell = grid.rows[i].cells[1];
var checkedAttrib = document.createAttribute('checked');
checkedAttrib.value = document.getElementById(RepeaterCheckboxid).checked; // this will take the value from 'RepeaterCheckboxid'
cell.childNodes[0].setAttributeNode(checkedAttrib); 

答案 1 :(得分:0)

尝试使用JQuery:

   $.each($(grid).find("tr"),function(){
        var chk = $(this).find("td:first").find(":checkbox");
        chk.attr("checked",$("#"+RepeaterCheckboxid).attr("checked"));
        chk.attr("disabled",!$("#"+RepeaterCheckboxid).attr("checked"));
    });  

答案 2 :(得分:0)

您可能需要使用其ClientID访问gridview,如:

$('#<%=GridView1.ClientID %>')

要获取gridview中的所有复选框,您可以使用.find(“input:checkbox”)并遍历每个复选框:

<script type="text/javascript">
function CheckUnCheckAll(chk) {
 $('#<%=GridView1.ClientID %>').find("input:checkbox").each(function () {
      if (this != chk) {
          this.checked = chk.checked;
        }
       });
  }
</script>

在标题复选框上调用CheckUnCheckAll(this)及其对象。

在此处查看示例:http://www.codegateway.com/2012/05/jquery-check-uncheck-all-checkboxes-in.html