2015-04-28 60 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

不错的一个...... ;-) –

相关问题