简体   繁体   English

当用户使用ASP.NET从Web窗体的DropDownList中选择其他控件时,如何显示TextBox

[英]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.

相关问题 如何使用JavaScript将值从父asp.net下拉列表传递到弹出框中的文本框 - How to pass value from parent asp.net dropdownlist to textbox in popup using javascript 如何使用ASP.NET WebForms中的JavaScript从FormView中的DropDownList获取文本到TextBox中? - How to get text into a TextBox from a DropDownList in FormView using JavaScript in ASP.NET WebForms? 用户使用JQuery切换到Asp.net文本框值时,如何用它替换以前的asp.net文本框值? - How can I fill an asp.net textbox with a previous asp.net textbox value when the user tabs to it using JQuery? 如何使用asp.net中的JavaScript阻止用户更改DropDownList值? - How to block User from changing DropDownList value using javascript in asp.net? 从DropDownList选择ASP.NET/Javascript填充文本框 - Populate a TextBox from a DropDownList selection ASP.NET/Javascript 用户在GridView ASP.NET中选择一行时如何自动选择同一ID的另一行 - How to automatically select other row of same ID when user selected a row in GridView ASP.NET 如何从 GridView 的 ASP.NET Web Z645024253191293266Z Z6450242531912981C3683CAE88DZ 内部同时上传多个文件? - How to upload multiple files simultaneously from inside GridView of ASP.NET Web Forms using JavaScript/jQuery? 如何使用asp.net C#中的Ajax和Web服务使用数据库中的数据填充动态创建的下拉列表 - how fill the dynamically created dropdownlist with data from database using ajax and web service in asp.net c# ASP.NET捆绑和缩小并从其他Web应用程序使用它 - ASP.NET Bundling and Minification and using it from other web application 从ASP.NET文本框中使用JavaScript - Using javascript from an ASP.NET textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM