简体   繁体   English

使用jquery ASP.Net取消选中checkboxlist的所有复选框

[英]check uncheck all checkbox of checkboxlist using jquery ASP.Net

i was trying to check uncheck all option of checkboxlist programmatically but my code is not working. 我试图以编程方式取消选中checkboxlist的所有选项,但我的代码无法正常工作。 here is snippet 这是片段

 $(document).ready(function () {
    $('#myLink').click(function () {
        $("INPUT[id^='chkFileds']").attr('checked', $('#chkFileds').is(':checked'));
    });
});

what is wrong in my code. 我的代码有什么问题。 i want that when user click on check hyperlink then all option of checkbox list will be checked and when click again on the same link then all option of checkbox list will be uncheck. 我希望当用户单击检查超链接时,将选中复选框列表的所有选项,当再次单击同一链接时,将取消选中复选框列表的所有选项。 please guide me how to do it. 请指导我如何做。 thanks 谢谢

UPDATE: 更新:

thanks for ur answer but i am not talking about check rather checkbox list what has many checkbox inside.it looks like....he i am giving the html of checkboxlist 感谢您的答复,但我不是在谈论复选框,而是复选框列表,里面有很多复选框。它看起来像....他正在给复选框列表的html。

<table style="font-weight:bold;" id="chkCountry">
    <tbody>
        <tr>
            <td>
                <input type="checkbox" value="GB" name="chkCountry$0" id="chkCountry_0">
                <label for="chkCountry_0">UK</label>
            </td>
            <td>
                <input type="checkbox" value="US" name="chkCountry$1" id="chkCountry_1">
                <label for="chkCountry_1">USA</label>
            </td>
        </tr>
    </tbody>
</table>

You markup is still missing some of the relevant code needed to answer the question properly, but, assuming that myLink is a checkbox, the code below will work. 您的标记仍然缺少正确回答问题所需的一些相关代码,但是,假设myLink是一个复选框,则下面的代码将起作用。

$(document).ready(function () {
   $('#myLink').click(function () {
      $("INPUT[id^='chkCountry_']").attr('checked', $('#myLink').is(':checked'));
   });
});

Here's a working fiddle . 是工作中的小提琴

Note : You're code is not working for several reasons. 注意 :您的代码由于某些原因而无法使用。 Mainly, your selector, chkFileds , doesn't relate to the markup you posted in any way. 主要是,选择器chkFileds与您以任何方式发布的标记chkFileds

Additional Info: 附加信息:

Here's a quick rundown of the attribute selector operators: 这是属性选择器运算符的简要介绍:

=  is exactly equal
!= is not equal
^= starts with
$= ends with
*= contains

If your selectors are correct, and they do seem a bit strange, maybe something like this: 如果您的选择器正确,并且看起来确实有些奇怪,也许是这样的:

$(document).ready(function () {
    $('#myLink').on('click', function () {
        var check = $('#chkFileds').is(':checked') ? false:true;
        $("INPUT[id^='chkFileds']").prop('checked', check);
    });
});

FIDDLE 小提琴

What exactly do you mean by "all" options, you are targeting an ID, and it's unique? 您所说的“所有”选项到底是什么意思,您定位的是ID,而且它是唯一的?

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

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