【问题标题】:Javascript -> Check/Uncheck gridview's checkboxes(ChildCheckbox) based on repeater's checkbox(ParentCheckbox)Javascript -> 基于中继器的复选框(ParentCheckbox)检查/取消选中gridview的复选框(ChildCheckbox)
【发布时间】:2012-05-13 23:22:14
【问题描述】:

我在下面有一个 javascript 函数 - 事实上,我在转发器中有一个复选框,在其中有一个 gridview,其中又有一些复选框。

我想根据中继器复选框的选择启用禁用 gridviewCheckboxes。

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

函数 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中的所有复选框?

如果大家有什么想法,请帮帮我。

【问题讨论】:

    标签: javascript jquery asp.net


    【解决方案1】:

    尝试使用 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】:

      试试这样:

      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); 
      

      【讨论】:

        【解决方案3】:

        您可能需要使用其 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

        【讨论】:

          猜你喜欢
          • 2018-09-15
          • 2012-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-20
          • 2014-05-05
          • 1970-01-01
          相关资源
          最近更新 更多