Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 计算复选框列表的数量,如果选中的项目超过1个,是否选中另一个复选框?_Jquery_Asp.net_Webforms - Fatal编程技术网

Jquery 计算复选框列表的数量,如果选中的项目超过1个,是否选中另一个复选框?

Jquery 计算复选框列表的数量,如果选中的项目超过1个,是否选中另一个复选框?,jquery,asp.net,webforms,Jquery,Asp.net,Webforms,我在asp.net的webform页面上有一个checkboxlist控件,如下代码所示: <div> <asp:CheckBoxList ID="CheckBoxList1" runat="server" CssClass="cbxlMulti"> <asp:ListItem Value="AAAA">AAAA</asp:ListItem> <asp:ListItem Value="BBBB">BBBB</asp

我在asp.net的webform页面上有一个checkboxlist控件,如下代码所示:

<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" CssClass="cbxlMulti">
    <asp:ListItem Value="AAAA">AAAA</asp:ListItem>
    <asp:ListItem Value="BBBB">BBBB</asp:ListItem>
    <asp:ListItem Value="CCCC">CCCC</asp:ListItem>
</asp:CheckBoxList>
</div>

AAAA
BBBB
中交
此代码由asp.net解析为HTML:

<div>
<table id="ContentPlaceHolder1_CheckBoxList1" class="cbxlMulti">
  <tbody>
    <tr>
      <td>
        <input id="ContentPlaceHolder1_CheckBoxList1_0" type="checkbox" name="ct100$ContentPlaceHolder1$CheckBoxList1$0" value="AAAA">
        <label for="ContentPlaceHolder1_CheckBoxList1_0">AAAA</label>
      </td>
    </tr>
    <tr>
      <td>
        <input id="ContentPlaceHolder1_CheckBoxList1_1" type="checkbox" name="ct100$ContentPlaceHolder1$CheckBoxList1$1" value="BBBB">
        <label for="ContentPlaceHolder1_CheckBoxList1_1">BBBB</label>
      </td>
    </tr>
    <tr>
      <td>
        <input id="ContentPlaceHolder1_CheckBoxList1_2" type="checkbox" name="ct100$ContentPlaceHolder1$CheckBoxList1$2" value="BBBB">
        <label for="ContentPlaceHolder1_CheckBoxList1_2">BBBB</label>
      </td>
    </tr>

AAAA
BBBB
BBBB
和另一个复选框:

<div>
<asp:CheckBox ID="CheckBox1" runat="server" CssClass="cbxSingle" Text="XXXX" />
</div>

我想知道CheckBoxlist1中所选项目的数量是否超过1,如果是,CheckBox1将被自动选中。我想使用jQuery实现这一点。

试试看

if ($('#CheckBoxList1 :checkbox:checked').length > 0){

    //check that
}
试一试

试试这个

if($('#CheckBoxList1 :checkbox:checked').length > 0){
    $('#CheckBox1').attr('checked','checked');
   //OR
    $('#CheckBox1').prop('checked', true);
   }
更新

$(function(){
    $("input[type=checkbox]").click(function () {
      var a= $("input[id^='ContentPlaceHolder1_CheckBoxList1']:checkbox:checked");

      if($(a).length > 0){
        $('#CheckBox1').attr('checked','checked');           
       }
      else
        $('#CheckBox1').removeAttr('checked');
    });
});
试试这个

if($('#CheckBoxList1 :checkbox:checked').length > 0){
    $('#CheckBox1').attr('checked','checked');
   //OR
    $('#CheckBox1').prop('checked', true);
   }
更新

$(function(){
    $("input[type=checkbox]").click(function () {
      var a= $("input[id^='ContentPlaceHolder1_CheckBoxList1']:checkbox:checked");

      if($(a).length > 0){
        $('#CheckBox1').attr('checked','checked');           
       }
      else
        $('#CheckBox1').removeAttr('checked');
    });
});

像这样试试看

$(function(){
    if ($('.cbxlMulti').find(':checkbox:checked').length > 1){
         $('.cbxSingle:checkbox').prop('checked',true);
    }
});
试试看

$(function(){
    if ($('.cbxlMulti').find(':checkbox:checked').length > 1){
         $('.cbxSingle:checkbox').prop('checked',true);
    }
});

您应该更改id,以便使用静态id,以便ASP.NET在呈现时不会更改您的id

<div>
    <asp:CheckBox ID="singleCheckBox" runat="server" CssClass="cbxSingle" ClientIDMode="Static" Text="XXXX" />
</div>

<script type="text/javascript">
    $('.cbxlMulti input[type=checkbox]').change(function () {
        $('#singleCheckBox').prop('checked', $('.cbxlMulti input[type=checkbox]:checked').length > 0);
    });
</script>

$('.cbxlMulti-input[type=checkbox]')。更改(函数(){
$('#singleCheckBox').prop('checked',$('.cbxlMulti-input[type=checkbox]:checked')。长度>0);
});
注意如果未选择任何项目,也将取消选中单个复选框。如果要在至少检查过一次后保持检查状态,则必须执行以下操作

<script type="text/javascript">
    $('.cbxlMulti input[type=checkbox]').change(function () {
        $('#singleCheckBox').prop('checked', $('#singleCheckBox').is(':checked') || $('.cbxlMulti input[type=checkbox]:checked').length > 0);
    });
</script>

$('.cbxlMulti-input[type=checkbox]')。更改(函数(){
$('#singleCheckBox').prop('checked',$('#singleCheckBox').is(':checked')|$('.cbxlMulti-input[type=checkbox]:checked')。长度>0);
});

您应该更改id,以便使用静态id,以便ASP.NET在呈现时不会更改您的id

<div>
    <asp:CheckBox ID="singleCheckBox" runat="server" CssClass="cbxSingle" ClientIDMode="Static" Text="XXXX" />
</div>

<script type="text/javascript">
    $('.cbxlMulti input[type=checkbox]').change(function () {
        $('#singleCheckBox').prop('checked', $('.cbxlMulti input[type=checkbox]:checked').length > 0);
    });
</script>

$('.cbxlMulti-input[type=checkbox]')。更改(函数(){
$('#singleCheckBox').prop('checked',$('.cbxlMulti-input[type=checkbox]:checked')。长度>0);
});
注意如果未选择任何项目,也将取消选中单个复选框。如果要在至少检查过一次后保持检查状态,则必须执行以下操作

<script type="text/javascript">
    $('.cbxlMulti input[type=checkbox]').change(function () {
        $('#singleCheckBox').prop('checked', $('#singleCheckBox').is(':checked') || $('.cbxlMulti input[type=checkbox]:checked').length > 0);
    });
</script>

$('.cbxlMulti-input[type=checkbox]')。更改(函数(){
$('#singleCheckBox').prop('checked',$('#singleCheckBox').is(':checked')|$('.cbxlMulti-input[type=checkbox]:checked')。长度>0);
});
试试这个

if($('#CheckBoxList1 :checkbox:checked').length > 0){
    $('#CheckBox1').attr('checked','checked');
   //OR
    $('#CheckBox1').prop('checked', true);
   }
假设一个变量,初始化为0,然后对所有选中的复选框和 添加到变量中。然后检查变量值是否大于1,然后检查 另一个复选框

var count=0;
$('#ContentPlaceHolder1_CheckBoxList1 input:checkbox:checked').each(function () {
count=count+1;
});

if(count>1)
{
$('#CheckBox1').attr('checked');
}
试试这个

if($('#CheckBoxList1 :checkbox:checked').length > 0){
    $('#CheckBox1').attr('checked','checked');
   //OR
    $('#CheckBox1').prop('checked', true);
   }
假设一个变量,初始化为0,然后对所有选中的复选框和 添加到变量中。然后检查变量值是否大于1,然后检查 另一个复选框

var count=0;
$('#ContentPlaceHolder1_CheckBoxList1 input:checkbox:checked').each(function () {
count=count+1;
});

if(count>1)
{
$('#CheckBox1').attr('checked');
}


我不能为你做这件事,但如果你向我们展示你迄今为止的尝试,我们可以帮助你。我不能为你做这件事,但如果你向我们展示你迄今为止的尝试,我们可以帮助你。谢谢你的回答。但它现在正在发挥作用。要使代码在加载页面时对复选框生效,是否需要添加$(document).ready(函数(){});代码之外?是的,您应该在
文档准备功能中编写代码,请参阅我的回答谢谢您的回答。但它现在正在发挥作用。要使代码在加载页面时对复选框生效,是否需要添加$(document).ready(函数(){});代码之外?是的,您应该在
文档准备功能中编写代码,请参见我的回答我更新了我的问题。我想在复选框列表更改时检测所选项目的数量,如果数量大于1,则选中单个复选框。怎么做?添加$(document).ready(函数(){});在代码之外?我认为if语句if($('$('input[id^='ContentPlaceholder 1\U CheckBoxList1']):checkbox:checked')。长度>0)有太多引号?ContentPlaceholder 1\u CheckBoxList1不在报价范围内。我更改了if语句,就好像($([输入[id^='ContentPlaceHolder1\u CheckBoxList1']:checkbox:checked”)。长度>0)我不知道这是否正确。但它不工作…给我一点时间,我会设置小提琴,小提琴网站不工作,现在肯定。我正在编辑上的第一个示例。当复选框列表更改时,它能够统计选中的项目。我需要做的是当计数大于1时,更改单个复选框。但是在这个例子中,复选框并不像我的帖子那样出现在一个表中,所以我仍然需要弄清楚如何编写选择器。是的,在我将clientdmode更改为static(如@Moe中的例子)之后,它工作得很好!非常感谢。我更新了我的问题。我想在复选框列表更改时检测所选项目的数量,如果数量大于1,则选中单个复选框。怎么做?添加$(document).ready(函数(){});在代码之外?我认为if语句if($('$('input[id^='ContentPlaceholder 1\U CheckBoxList1']):checkbox:checked')。长度>0)有太多引号?ContentPlaceholder 1\u CheckBoxList1不在报价范围内。我更改了if语句,就好像($([输入[id^='ContentPlaceHolder1\u CheckBoxList1']:checkbox:checked”)。长度>0)我不知道这是否正确。但它不工作…给我一点时间,我会设置小提琴,小提琴网站不工作,现在肯定。我正在编辑上的第一个示例。当复选框列表更改时,它能够统计选中的项目。我需要做的是当计数大于1时,更改单个复选框。但是在这个例子中,复选框并不像我的帖子那样出现在一个表中,所以我仍然需要弄清楚如何编写选择器。是的,在我将clientdmode更改为static(如@Moe中的例子)之后,它工作得很好!非常感谢。谢谢你的回答。我将单个复选框的ID更改为“cbxSingle1”,但ASP.NET将ID解析为“cphmaincontent_cbxSingle1”,因为我使用的是内容占位符。然后我将“#cbxSingle1”更改为“#cphmainante_cbxSingle1”,它仍然不工作将clientdmode更改为static,就像在我的示例中一样感谢Moe,这在