[英]How TextBox is shown when user select other from DropDownList in web forms using ASP.NET
I have asp:DropDownList
and my requirement is when user select other from list a TextBox
is shown to user. 我有
asp:DropDownList
,我的要求是当用户从列表中选择其他时,向用户显示一个TextBox
。 But initially TextBox
is hidden. 但是最初,
TextBox
是隐藏的。
I write below code to hide TextBox
when page is loaded. 我写下面的代码以在页面加载时隐藏
TextBox
。
txtOtherArea.Attributes.Add("style", "display:none")
But when I try to show this TextBox
using JQuery, nothing happened. 但是,当我尝试使用JQuery显示此
TextBox
,什么也没发生。 Below is my JQuery code. 下面是我的JQuery代码。
$(function () {
$("#ddlUserArea").click(function () {
$("#txtOtherArea").fadeIn("slow");
});
});
I also try to use show()
instead of fadeIn("slow")
but there is no option to use show()
method. 我也尝试使用
show()
而不是fadeIn("slow")
但是没有使用show()
方法的选项。
Then I try below code to hide TextBox
initially. 然后,我尝试下面的代码来最初隐藏
TextBox
。
<script type="text/javascript">
$(function () {
$("#txtOtherArea").fadeOut(1000);
});
</script>
I write above code in head section of Master page. 我在母版页的首页部分中编写了以上代码。 But nothing happened.
但是什么也没发生。
I read other topics and question on this forum but don't understand may be because of much complexity. 我在该论坛上阅读了其他主题和问题,但不了解可能是因为过于复杂。 I am new in programming and just need to show
TextBox
when user select Other from DropDownList
. 我是编程新手,当用户从
DropDownList
选择Other时只需要显示TextBox
。 Please help. 请帮忙。
Because you are using server side id of control, you should use client side id as shown below. 因为您使用的是控件的服务器端ID,所以应使用客户端ID,如下所示。
$(function () {
$("#ddlUserArea").click(function () {//YOU SHOULD USE .change() instead
$("#<%=txtOtherArea.ClientID%>").fadeIn("slow");
});
});
BETTER APPROACH 更好的方法
$(function () {
var ddl=$("#<%=ddlUserArea.ClientID%>");
var txt=$("#<%=txtOtherArea.ClientID%>");
ddl.change(function () {
if(ddl.val()=="Other"){
txt.fadeIn("slow");
}
});
});
Don't use magic string for id, instead use ClientID
generated by asp.net for your controls : 不要将魔术字符串用作id,而
ClientID
asp.net生成的ClientID
用于控件:
var ddlUserArea = "<%=ddlUserArea.ClientID %>";
var txtOtherArea = "<%=txtOtherArea.ClientID %>";
and for dropdown you have to use change event: 对于下拉列表,您必须使用change事件:
$("#"+ddlUserArea).change(function () {
$("#"+txtOtherArea).fadeIn("slow");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.