简体   繁体   English

如何使用JavaScript根据文本框显示或隐藏面板?

[英]How to show or hide a panel based on text box entry using javascript?

I have a text box in which user enters a number. 我有一个文本框,用户可以在其中输入数字。 I want to show a new panel when user enters a value bigger than 1 in the text box but hide it when the number is 1. I'm using Onkeyup function in javascript and also tried onblur it doesn't work. 我想在用户在文本框中输入大于1的值时显示一个新面板,但当数字为1时将其隐藏。我在javascript中使用Onkeyup函数,并且还尝试过onblur无效。

This is the part that I get the value. 这是我获得价值的部分。

<tr>
    <td class="ColWidth">
        <span class="red" style="color: #FF0000; font-weight: bold;"></span>
        <asp:Label ID="lblUnitsAffedted" runat="server" 
             Text="Total number of untis affected: ">
        </asp:Label>
    </td>
    <td class="InputCallWidth">
        <asp:TextBox ID="txtUnitsAffected" runat="server" Height="25px" Width="256px" 
              onkeyup="getUnitsAffected();">
        </asp:TextBox>           
    </td>
</tr>

This is the part where I want to display when number of units is larger than 1: 当单位数大于1时,这是我要显示的部分

<tr>
    <td colspan="4">
        <div id="divUnitsAffected" runat="server" visible="true">
            <table width="100%" border="0" cellspacing="0" class="tbl">
                <tr>
                    <td colspan="3" style="color:black;">
                        <h4>
                            please provide Names & Unit numbers
                        </h4>
                    </td>
                </tr>
            </table>
        </div>
    </td>
</tr>

And here is my Java Script method. 这是我的Java Script方法。

<script language="javascript" type="text/javascript">

     function getUnitsAffected() {
         //Get the Textbox control
         var UnitsAffected = document.getElementById("<%=txtUnitsAffected.ClientID %>");
         var DivUnitsAffected = document.getElementById('DivUnitsAffected');

         if (UnitsAffected.value > 1){
             DivUnitsAffected.style.display = "block";
         }
         else {
             DivUnitsAffected.style.display = "none";
         }
     }
</script>

Instead of onKeyUp event use the onblur or focusout event. 代替onKeyUp事件,请使用onblur或focusout事件。 Then you can fetch & check the value of your input control and based on the value you will be able set the panel visibility. 然后,您可以获取并检查输入控件的值,并根据该值可以设置面板可见性。

Further, you can use parseFloat() JavaScript function to get the decimal form of the value being entered and then check for the condition, whether it is less than or equal to 1 OR greater than 1. 此外,您可以使用parseFloat()JavaScript函数获取输入值的十进制形式,然后检查条件,该条件是小于还是等于1或大于1。

Try the below codes 试试下面的代码

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

  <table>
   <tbody>
       <tr>
           <td>
               <asp:TextBox ID="txtUnitsAffected" runat="server" Height="25px" Width="256px" 
          onblur="getUnitsAffected(this);">
    </asp:TextBox>
           </td>
       </tr>
       <tr>
           <td>
               <div id="divUnitsAffected">
                   Testing
               </div>
           </td>
       </tr>
   </tbody>
</table>

   <script type="text/javascript">
       function getUnitsAffected(txtbx)
       {
           var num = parseInt(txtbx.value);
           var div = document.getElementById('divUnitsAffected');

           if(num > 1)
           {
               div.style.display = "block";
           }
           else
           {
               div.style.display = "none";
           }
       }
   </script>
</asp:Content>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM