将 asp:textbox 可见属性更改为 true 的 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17102290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:06:11  来源:igfitidea点击:

Javascript to change asp:textbox visible property to true

javascriptc#asp.net

提问by user2484849

I am trying to create an form in which a textbox should be shown or hidden based on the dropdown & also validate the textbox if it is shown only.

我正在尝试创建一个表单,其中应根据下拉列表显示或隐藏文本框,并且如果仅显示文本框,则还验证该文本框。

Can anyone help me in this?

任何人都可以帮助我吗?

<asp:DropDownList 
    ID="txt_days" 
    CssClass="txt_drpbx" 
    runat="server"  
    siz="10" 
>
    <asp:ListItem Text="Select" Value="0"></asp:ListItem>
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator 
    ID="RequiredFieldValidator6" 
    runat="server" 
    ControlToValidate="txt_days" 
    ForeColor="Red" 
    ErrorMessage="Please Select No of days on leave" 
    InitialValue="0" 
    ValidationGroup="fdbk_chk"
>*</asp:RequiredFieldValidator>
<asp:TextBox 
    ID="txt_todate" 
    runat="server" 
    ReadOnly="true" 
    Visible="false" 
    CssClass="txt_bx"
></asp:TextBox>
<asp:RequiredFieldValidator 
    ID="RequiredFieldValidator1" 
    runat="server" 
    ControlToValidate="txt_todate" 
    ForeColor="Red" 
    ErrorMessage="Please Select Feedback Given For" 
    ValidationGroup="fdbk_chk"
>*</asp:RequiredFieldValidator>
<asp:Button 
    ID="btn_submit" 
    Text="Submit" 
    CssClass="btn_button" 
    runat="server" 
    onclick="btn_submit_Click" 
    ValidationGroup="fdbk_chk" 
/>
<asp:ValidationSummary 
    ID="ValidationSummary1" 
    runat="server" 
    HeaderText="Some fields are missing" 
    ShowMessageBox="True" 
    ShowSummary="False" 
    ValidationGroup="fdbk_chk" 
/>

回答by Altaf Sami

Try this:

试试这个:

<script>

    function ShowHideTextBox(ddlId)
    {
         var ddl = document.getElementById(ddlId.id);

         if(ddl.value == 1)  //your condition
         {
             document.getElementById('txt_todate').style.display = 'none';
          }
         else
         {
             document.getElementById('txt_todate').style.display = '';

         }
    } 

<asp:DropDownList ID="txt_days" CssClass="txt_drpbx" runat="server"  siz="10" onchange="ShowHideTextBox(this);" 

<asp:TextBox ID="txt_todate" runat="server" ReadOnly="true" Visible="false" CssClass="txt_bx"></asp:TextBox> 

回答by CocLn

Instead of using visible, set its css to display:none

而不是使用可见,将其 css 设置为 display:none

  <asp:TextBox ID="txt_todate" runat="server" style="display:none;" CssClass="txt_bx">
  </asp:TextBox> 

and add your dropdownlist onchange

并添加您的下拉列表 onchange

 <asp:DropDownList ID="ddlDays" CssClass="txt_drpbx" runat="server" onchange="ShowHide();">
   <asp:ListItem Text="Select" Value="0"></asp:ListItem>
   <asp:ListItem Text="Show" Value="1"></asp:ListItem>
   <asp:ListItem Text="Hide" Value="2"></asp:ListItem>
 </asp:DropDownList>

in script;

在脚本中;

 <script  type="text/javascript">
   function ShowHide(){
    var value = document.getElementById('ddlDays').value;
    var theControl = document.getElementById("txt_todate");
    if(value != 1){  // only when value is 1 it must show
       theControl.style.display = "none";
     }
    else{
       theControl.style.display = "block";
    }
   }
  </script>

回答by JulyOrdinary

I tried display none at my end it was not working, but I was successful with visibility property. So, try this:

我尝试在最后显示 none 它不起作用,但我成功地使用了可见性属性。所以,试试这个:

function Validate()
{
     var dropdown = document.getElementById('ID of the dropdown');

     if(Condition which you want) 
     {
         document.getElementById('txt_todate').style.visibility= true;
      }
     else
     {
         document.getElementById('txt_todate').style.visibility= false;

     }
} 

回答by Fran Cerezo

Control id on server side is not the same in client side. To be the same and to can use ID with javascript you must add to your server controls ClientIDMode="Static"

服务器端的控件 ID 与客户端的不同。为了相同并且可以将 ID 与 javascript 一起使用,您必须添加到您的服务器控件 ClientIDMode="Static"

As example:

例如:

<asp:DropDownList ID="ddlDays" ClientIDMode="Static" CssClass="txt_drpbx" runat="server" onchange="ShowHide();">