2010-09-02 65 views
0

我正在使用在其中一列中具有单选按钮的gridview。它被用作真/假标志。无论如何,由于我的项目的性质,我需要JavaScript来取消任何单选按钮,当我检查另一个。这工作正常,除了我的gridview包装在更新面板中。我正在关注在自动复位上重新设置jQuery,但发生的事情发生在回发之后,删除我选中的单选按钮并且不会将其放回。如果我不使用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'); 
    }); 

}); 

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

回答

0

我解决了这个问题,通过改变我的一些t-sql代码,而不必使用jquery。

0

几件事情:

$(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 
+0

单选按钮点击我试图取消所有单选按钮,然后检查只是一个,我点击。它应该看起来像上一行代码,没有看到它。 – 2010-09-02 04:04:48

0

假设你设置的名称单选按钮(而不是ID)是相同的,HTML会自动进行选择独家为你没有任何jQuery的需要。这是一个收音机框与复选框的不同之处。我会首先考虑这一点,因为如果你设置正确的话,你不需要大量的JS来完成HTML内置的功能。

+0

我不能那样做,因为我使用gridview,它有一个编辑模板和它的常规模板。在这种情况下,只有JavaScript可以成功删除其他收音机检查状态。 – 2010-09-02 04:12:32

0

我改变了你这样的代码:

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

,它为我的作品!

0

试试吧

对单选按钮

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); }); 
    }); 
} 
相关问题