【问题标题】:Disable all check boxes inside a table with jquery使用jquery禁用表内的所有复选框
【发布时间】:2014-02-20 20:45:44
【问题描述】:

单击同一表格内的超链接时,我需要禁用表格单元格内的所有复选框。

我正在使用以下 jquery 代码来选择嵌套在表格中的所有复选框。

$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');
$($el).attr('checked', true);

由于某种原因,这段代码无法正常工作。

谁能告诉我如何解决它?

【问题讨论】:

    标签: jquery checkbox


    【解决方案1】:
    $('table input[type=checkbox]').attr('disabled','true');
    

    如果你有表的 id

    $('table#ID input[type=checkbox]').attr('disabled','true');
    

    【讨论】:

    • 我注意到人们应该把 write true 而不是'true'。这有效: $('table input[type=checkbox]').attr('disabled',true) 但不是这个: $('table input[type=checkbox]').attr('disabled','true' );
    • 帮助我,让我免于写问题。谢谢你。 +1
    【解决方案2】:

    禁用?

    $("a.clickme").click(function(){
      $(this)                    // Link has been clicked
        .closest("td")           // Get Parent TD
        .find("input:checkbox")  // Find all checkboxes
        .attr("disabled", true); // Disable them
    });
    

    或已检查?

    $("a.clickme").click(function(){
      $(this)                    // Link has been clicked
        .closest("td")           // Get Parent TD
        .find("input:checkbox")  // Find all checkboxes
        .attr("checked", false); // Uncheck them
    });
    

    【讨论】:

      【解决方案3】:

      您的代码可以简单得多:

      $el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');
      

      可能是:

      $el = $(this).parents('table:first :checkbox');
      

      然后禁用它们:

      $el.attr('disabled', 'disabled');
      

      或检查它们:

      $el.attr('checked', 'checked');
      

      或取消选中它们:

      $el.removeAttr('checked');
      

      或启用它们:

      $el.removeAttr('disabled');
      

      【讨论】:

        【解决方案4】:

        另请参阅:selector/checkbox

        jQuery("#hyperlink").click(function() {
          jQuery('#table input:checkbox').attr('disabled', true);
          return false;
        });
        

        【讨论】:

          【解决方案5】:

          // 启用/禁用所有复选框

          $('#checkbox').click(function() {
          
              var checked = $(this).attr('checked');
              var checkboxes = '.checkboxes input[type=checkbox]';
          
              if (checked) {
                  $(this).attr('checked','checked');
                  $(checkboxes).attr('disabled','true');
              } else {
                  $(this).removeAttr('checked');
                  $(checkboxes).removeAttr('disabled');
              }
          });
          

          【讨论】:

            【解决方案6】:

            这是我的解决方案

            // Action sur le checkbox
                    $("#tabEmployes thead tr th:first input:checkbox").click(function() {
                        var checked = $(this).prop('checked');
                        $("#tabEmployes tbody tr td:first-child input:checkbox").each(function() {
                            $(this).prop('checked',checked);
                        });
                    });
            

            【讨论】:

              【解决方案7】:

              ------------------- HTML 代码如下 ------------- ------------------

              <table id="myTable">
                  <tr>
                      <td><input type="checkbox" checked="checked" /></td>
                      <td><input type="checkbox" checked="checked" /></td>
                      <td><input type="checkbox" /></td>
                      <td><input type="checkbox" /></td>
                  </tr>
              </table>
              
              <input type="button" onclick="callFunction()" value="Click" />
              

              ------------------- JQuery 代码如下 ------------- ----------------

                  <script type="text/javascript">
                  function callFunction() {
                      //:
                      $('table input[type=checkbox]').attr('disabled', 'true');
                  }
              </script>
              

              【讨论】:

              • 您好,点击按钮后,您会看到当这个JS函数调用时,四个复选框都将被禁用。谢谢。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-09-09
              • 2012-09-03
              • 1970-01-01
              相关资源
              最近更新 更多