2015-04-28 96 views
0

我的jQuery代碼在頁面加載時未觸發。然而,一旦頁面加載,然後如果我切換單選按鈕它的作品。我對jQuery和ASP.Net相當陌生,因此我不知道哪裏出錯了。我試過onload="",但我無法弄清楚我的生活如何調用默認的jQuery函數,該函數在那個onload代碼塊中沒有任何名稱。jquery選中頁面加載時未觸發

$(document).ready(function() { 
    $('#<%=RadioButtonList1.ClientID %>').click(function() { 
     var SelectedValue = $('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val(); 
     if (SelectedValue == 1) { 
      //If cash is selected then hide the Div 
      $('#DropDownList1').css("display", "none"); 
      $('#MetaType').css("display", "none"); 
      $('#TextBox7').css("display", "none"); 
      $('#MetaSize').css("display", "none"); 
      //or you can simply use jQuery hide method to hide the Div as below: 
      //$('#dvShowHide').hide();   
     } 
     else { 
      //If Cheque is selected then show the Div 
      $('#DropDownList1').css("display", "block"); 
      $('#MetaType').css("display", "block"); 
      $('#TextBox7').css("display", "block"); 
      $('#MetaSize').css("display", "block"); 
      //or you can simply use jQuery show method to show the Div as below: 
      //$('#dvShowHide').show(); 
      //Clear textboxes 
      $('#<%=TextBox7.ClientID %>').val(''); 

      //Set focus in bank name textbox 
      $('#<%=TextBox7.ClientID %>').focus(); 
     } 
    }); 
}); 
<asp:RadioButtonList ID="RadioButtonList1" runat="server"> 
    <asp:ListItem Value="0">Yes</asp:ListItem> 
    <asp:ListItem Selected="True" Value="1">No</asp:ListItem> 
</asp:RadioButtonList> 
+0

?我看到的jQuery代碼是在點擊事件,這就是爲什麼它不執行onload –

回答

0

要調用click處理程序在頁面負載,可以trigger()事件:

$('#<%=RadioButtonList1.ClientID %>').click(function() { 
    // your code here... 
}).click(); // < trigger the event on load 

您還可以使用.trigger('click');

另外,您可以將您的邏輯中分離出來它自己的功能,這是在點擊按鈕和頁面加載時調用的:

$(document).ready(function() { 
    $('#<%=RadioButtonList1.ClientID %>').click(checkState); // onclick 
    checkState(); // onload 
}); 

function checkState() { 
    var SelectedValue = $('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val(); 
    if (SelectedValue == 1) { 
     //If cash is selected then hide the Div 
     $('#DropDownList1').css("display", "none"); 
     $('#MetaType').css("display", "none"); 
     $('#TextBox7').css("display", "none"); 
     $('#MetaSize').css("display", "none"); 
     //or you can simply use jQuery hide method to hide the Div as below: 
     //$('#dvShowHide').hide();   
    } 
    else { 
     //If Cheque is selected then show the Div 
     $('#DropDownList1').css("display", "block"); 
     $('#MetaType').css("display", "block"); 
     $('#TextBox7').css("display", "block"); 
     $('#MetaSize').css("display", "block"); 
     //or you can simply use jQuery show method to show the Div as below: 
     //$('#dvShowHide').show(); 
     //Clear textboxes 
     $('#<%=TextBox7.ClientID %>').val(''); 

     //Set focus in bank name textbox 
     $('#<%=TextBox7.ClientID %>').focus(); 
    } 
} 
+0

謝謝他的工作就像一個魅力:)我真的很感謝大家的幫助誰回答:) matty和tushar :)謝謝。 – Frown

0

運行在頁面加載這個邏輯,你最好提取出來成一個函數,然後調用函數,是這樣的:

$(function() { 
    setValue(); 
    $('#<%=RadioButtonList1.ClientID %>').click(setValue); 
}); 


function setValue() 
{ 
    var SelectedValue = $('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val(); 
    if (SelectedValue == 1) { 
     //If cash is selected then hide the Div 
     $('#DropDownList1').css("display", "none"); 
     $('#MetaType').css("display", "none"); 
     $('#TextBox7').css("display", "none"); 
     $('#MetaSize').css("display", "none"); 
     //or you can simply use jQuery hide method to hide the Div as below: 
     //$('#dvShowHide').hide();   
    } 
    else { 
     //If Cheque is selected then show the Div 
     $('#DropDownList1').css("display", "block"); 
     $('#MetaType').css("display", "block"); 
     $('#TextBox7').css("display", "block"); 
     $('#MetaSize').css("display", "block"); 
     //or you can simply use jQuery show method to show the Div as below: 
     //$('#dvShowHide').show(); 
     //Clear textboxes 
     $('#<%=TextBox7.ClientID %>').val(''); 

     //Set focus in bank name textbox 
     $('#<%=TextBox7.ClientID %>').focus(); 
    } 
} 
你要哪個代碼執行的onload
+0

不錯的一個...... ;-) –

相關問題