2014-02-05 145 views
0

我有以下cenario:ASP.net恢復複選框狀態

<asp:HiddenField ID="tab_indexer" runat="server" Value="tab_id" /> 
<ul class="tabs"> 
    <li> 
     <input type="radio" name="tabs" id="tab_id" /> 
     <label for="tab_id">Identificação</label> 
     <div class="tabcontainer"></div> 
    </li> 
    <li> 
     <input type="radio" name="tabs" id="tab_message" /> 
     <label for="tab_message">Mensagem</label> 
     <div class="tabcontainer"></div> 
    </li> 
</ul> 

不改變頁面的結構(因爲我的ASP.net應用程序已經在此建工作),我需要,當輸入[type =「radio」]更改checked屬性,它將其id保存在「tab_indexer」hiddenField中,並且當頁面加載時,通過Id存儲在HiddenField值中的輸入[type =「radio」]並設置它檢查。

這是什麼Javascript(我已經綁定了JQuery js包在頁面上)?

到目前爲止,這要歸功於另一個問題,我做了#1,我得到:

$(document).ready(function(){ 
    $("input[type=checkbox]").on('click', function(){$("tab_indexer").attr("value", $(this).attr("id"));}); 
    $($("tab_indexer").val()).attr('checked', 'checked'); 
}); 

但是,如果沒有sucess。

+0

還有像'型= radio'等檢查我的答案非常 –

回答

0

由於tab_indexer是一個ASP.NET控件,您必須使用Control.ClientID

在jQuery中,當你想使用elemnts使用ID Selector (「#id」),即前綴ID與#

要選擇更改您的代碼

$(document).ready(function() { 
    //Get tab_indexer element 
    var tab_indexer = $("#" + <%= tab_indexer.ClientID %>); 
    //Set checkbox checked with value stored in tab_indexer 
    $('#' + tab_indexer.val()).prop('checked', 'checked'); 

    $("input[type=checkbox]").on('click', function() { 
     $(tab_indexer).val($(this).attr("id")); 
    }); 
}); 
+0

感謝很多東西,我會看到throught ...我beleave這就是我正在尋找的......真是太棒了Satpal – SammuelMiranda

0

您需要檢查type=radio,並需要使用適當的id選擇器tab_indexer,即$("#<%=tab_indexer.ClientID")和一個邏輯來設置值

$(document).ready(function(){ 
    //bind event click 
    $("input[type=radio]").on('click', function() 
      {$("#<%=tab_indexer.ClientID").attr("value", $(this).attr("id"));}); 

    //set selected radio button checked 
    var selectedRadioId=$("#<%=tab_indexer.ClientID").val(); 
    $('input[type="radio"]').each(function(){ 
    if(this.value==selectedRadioId){ 
     this.checked=true; 
    } 
    }); 

});