【问题标题】:jQuery check/uncheck problemsjQuery检查/取消检查问题
【发布时间】:2011-04-07 02:59:21
【问题描述】:

我正在使用在其中一列中有一个单选按钮的 gridview。它用作真/假标志。无论如何,由于我项目的性质,当我检查另一个单选按钮时,我需要 javascript 取消选中任何单选按钮。这很好用,除了我的 gridview 包含在更新面板中。我正在处理在自动回发时重新初始化 jQuery,但正在发生的事情是在回发时,删除我选中的单选按钮而不是把它放回去。如果我不使用 jQuery,它可以正常工作(但是我没有获得按钮排他性),所以我认为它是我的代码。这是我的代码。

<script type="text/javascript">
$(document).ready(function () {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    function EndRequestHandler(sender, args) {
        $("input").click(function () {
            $("input").removeAttr('checked');
            $(this).attr('checked','checked');
        });
    }
    $("input").click(function () {
        $("input").removeAttr('checked');
        $(this).attr(':checked');
    });

});

有没有更好的方法来编写这段代码?谢谢。

【问题讨论】:

    标签: asp.net jquery radio-button


    【解决方案1】:

    几件事:

    $(this).attr(':checked'); //this doesnt look right.. what are you trying to do here?
    
    $('input') // this will target all input fields on the page, all radio buttons, textboxes etc. i don't think u want that. try giving the radio's IDs and use the ID selector to select them in jQuery
    

    【讨论】:

    • 单选按钮单击我试图取消选中所有单选按钮,然后只检查我单击的那个。它应该看起来像上一行代码,没有看到它。
    【解决方案2】:

    假设您将单选按钮的名称(而不是 ID)设置为相同,HTML 将自动为您提供独占选择,无需任何 jQuery。这就是单选框​​与复选框的不同之处。我会先研究一下,因为如果设置正确,您不需要一堆 JS 来执行 HTML 内置的功能。

    【讨论】:

    • 我不能那样做,因为我使用的是gridview,它有一个编辑模板和它的常规模板。在这种情况下,只有 javascript 可以成功删除其他无线电检查状态。
    【解决方案3】:

    我通过更改我的一些 t-sql 代码并且根本不必使用 jquery 解决了这个问题。

    【讨论】:

      【解决方案4】:

      我这样修改了你的代码:

      if( $('input.any-radio').length ){ $('input.any-radio').change(function(){ $('input.any-radio').removeAttr('checked'); $(this).attr('checked', 'checked'); });}

      它对我有用!

      【讨论】:

        【解决方案5】:

        试试看

        单选按钮

        function toggleCheckRadioButton(sender) {
            var res;
            if ($(sender).attr('previousValue') == 'true') {
                $(sender).attr('checked', false)
                res = false;
            } 
            else {
                $(sender).attr('checked', true);
                res = true;
            }
        
            $('form input[type="radio"][name$="' + $(sender).attr('name') + '"]').each(function () {
                $(this).attr('previousValue', false);        
            });
        
            $(sender).attr('previousValue', res);    
        }
        

        对于电台列表

        function initToggleCheckRadioList(sender) {
            $(sender).find('input[type="radio"]').each(function () {
                $(this).click(function () { toggleCheckRadioButton(this); });
            });
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-04
          • 2010-09-19
          • 1970-01-01
          • 2013-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多