简体   繁体   English

如何使用jQuery在div状态内切换复选框?

[英]how to toggle checkbox inside a div status using jquery?

<table>
 <tbody>
    <tr>
     <td>
       <span>A_Group</span>
        <input type="hidden" value="1" id="ContentPlaceHolder1_rptFleet_hiddenFleetID_0" name="ctl00$ContentPlaceHolder1$rptFleet$ctl00$hiddenFleetID">
     </td>
     <td>
       <span name="chkGroupName">
        <input type="checkbox" onclick="jqCheckAll3(ContentPlaceHolder1_rptFleet_chkFleetName_0,1 );" name="ctl00$ContentPlaceHolder1$rptFleet$ctl00$chkFleetName" id="ContentPlaceHolder1_rptFleet_chkFleetName_0">
        <label for="ContentPlaceHolder1_rptFleet_chkFleetName_0">Select All</label>
       </span>
    </td>
   </tr>
   <tr>
    <td>
      <div id="1"><table cellspacing="5" cellpadding="5" id="ContentPlaceHolder1_rptFleet_chkListDevice_0">
         <tbody>
           <tr>
               <td>
                   <input type="checkbox" value="1" name="ctl00$ContentPlaceHolder1$rptFleet$ctl00$chkListDevice$0" id="ContentPlaceHolder1_rptFleet_chkListDevice_0_0_0">
                   <label for="ContentPlaceHolder1_rptFleet_chkListDevice_0_0_0">name 2</label>
               </td>
               <td>
                  <input type="checkbox" value="2" name="ctl00$ContentPlaceHolder1$rptFleet$ctl00$chkListDevice$1" id="ContentPlaceHolder1_rptFleet_chkListDevice_0_1_0">
                  <label for="ContentPlaceHolder1_rptFleet_chkListDevice_0_1_0">name 4</label>
               </td>
               <td>
                   <input type="checkbox" value="3" name="ctl00$ContentPlaceHolder1$rptFleet$ctl00$chkListDevice$2" id="ContentPlaceHolder1_rptFleet_chkListDevice_0_2_0">
                   <label for="ContentPlaceHolder1_rptFleet_chkListDevice_0_2_0">name 4</label>
               </td>
             </tr>
         </tbody>
        </table>
       </div>
      </td>
     </tr>
    </tbody>
</table>


function jqCheckAll3(id, pID) {
$("#" + pID + " :checkbox").attr('checked', $('#' + id).is(':checked'));
}

I would like to check/uncheck all checkboxes inside div=1 when user clicked on checkbox select all 当用户单击复选框时,我想选中/取消选中div = 1内的所有复选框

Your IDs need to be quoted, this: 您的ID需要加引号,这是:

onclick="jqCheckAll3(ContentPlaceHolder1_rptFleet_chkFleetName_0,1 );

needs to be: 需要是:

onclick="jqCheckAll3('ContentPlaceHolder1_rptFleet_chkFleetName_0', '1');

There are other issues, like id="1" isn't valid in HTML4, but the lack of quotes throwing a script error is your current issue. 还有其他问题,例如id="1"在HTML4中无效,但是缺少引号引发脚本错误是您当前的问题。


A simpler way overall would be giving that checkbox a class instead via CssClass , like class="checkAll" , then you could do this: 总体上,更简单的方法是通过CssClass将该复选框提供一个类,例如class="checkAll" ,那么您可以这样做:

<script type="text/javascript">
$(function() {
  $("input.checkAll").change(function() {
    $(this).closest("tr").next().find(":checkbox").attr("checked", this.checked);
  });
});
</script>

Then this one set of code works for all these situations in the page (assuming the markup/layout is the same). 然后,这组代码适用于页面中的所有这些情况(假设标记/布局相同)。

I would put this together a little differently. 我会把它放在一起有点不同。 If I understand your problem you are trying to either check all checkboxes or uncheck all checkboxes based on a single checkbox (the select all one). 如果我了解您的问题,则您要尝试选中所有复选框或取消基于一个复选框(选中全部)的所有复选框。

First things first, as Nick pointed out, you shouldn't be using "1" as your ID for the div. 首先,正如Nick指出的那样,您不应使用“ 1”作为div的ID。 and also you can use a single selection statement to get all the checkboxes that are a child of any specific div like this... 而且您也可以使用单个选择语句来获取属于任何特定div子级的所有复选框,如下所示:

//notice I changed the name of the div to myDiv
var isChecked = $(this).attr('checked');
$('#myDiv input:checkbox').attr('checked', isChecked);

This selects the div by id, then any inputs that are checkboxes. 这将按id选择div,然后选择复选框中的所有输入。 I get the checked value of the "select all" check box (this is called on the onclick event) and use that value to set the values of the checkboxes. 我得到“全选”复选框的选中值(在onclick事件上调用),并使用该值设置复选框的值。

I would also move the wiring of the event to the document.ready event instead of doing the function call in the markup, but that is just more preference. 我还将事件的连线移至document.ready事件,而不是在标记中进行函数调用,但这只是更多的选择。

Here is a link to everything working in a jsfiddle 这是所有在jsfiddle中工作的链接

Hope this help... 希望能有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM