代码之家  ›  专栏  ›  技术社区  ›  The Muffin Man

jquery选中/取消选中问题

  •  0
  • The Muffin Man  · 技术社区  · 15 年前

    我正在使用一个在其中一列中有一个单选按钮的网格视图。它被用作真/假标志。无论如何,由于我的项目的性质,当我选中另一个单选按钮时,我需要JavaScript取消选中任何单选按钮。除了我的GridView被包装在一个更新面板中之外,这一切都可以正常工作。我正在重新启动autopost上的jquery,但是发生的事情是在post back上,删除我选中的单选按钮,而不是将其放回。如果我不使用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');
        });
    
    });
    

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

    5 回复  |  直到 11 年前
        1
  •  0
  •   Moin Zaman    15 年前

    以下几点:

    $(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
  •  0
  •   Parrots    15 年前

    假设您将单选按钮的名称(而不是ID)设置为相同,HTML将自动为您排他性地进行选择,而不需要任何jquery。这就是为什么收音机和复选框不同。我会先研究一下,因为如果设置正确的话,你不需要一堆JS来做HTML内置的工作。

        3
  •  0
  •   The Muffin Man    15 年前

    我通过更改一些T-SQL代码解决了这个问题,根本不需要使用jquery。

        4
  •  0
  •   P.M    15 年前

    我像这样修改了你的代码:

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

    它对我有用!

        5
  •  0
  •   Mc Natit    11 年前

    试试看

    用于单选按钮

    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); });
        });
    }
    
    推荐文章