2010-09-02 62 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); }); 
    }); 
} 
相關問題