简体   繁体   English

当上一个文本框包含文本时,如何使文本框可见

[英]How to make textbox visible when previous textbox contains text

I have a WebForm in which i need to place around 30 textboxes mainly to enter barcode scanned data. 我有一个WebForm,其中需要放置约30个文本框,主要用于输入条形码扫描的数据。 I am making only the first textbox visible and i want the next textbox to be visible only when the previous textbox is filled with some text. 我仅使第一个文本框可见,并且我希望只有当前一个文本框填充了某些文本时,下一个文本框才可见。 I tried using 'If' condition as well in the textbox on selected change but it doesn't work. 我也尝试在选定更改的文本框中使用“如果”条件,但它不起作用。 Any solutions? 有什么办法吗?

You can resolve your problem by Jquery. 您可以通过Jquery解决您的问题。 I have make a sample code where i have take four Textbox. 我有一个示例代码,其中我有四个Textbox。 Initially only first text box is visible in Web form, when user enter some values in first TextBox next Textbox is automatically display if Previous textbox have a value if not next textbox is not visible. 最初,在Web窗体中只有第一个文本框可见,当用户在第一个TextBox中输入一些值时,如果Previous textbox具有值(如果下一个textbox不可见),则自动显示next Textbox。

Sample code is given below : 示例代码如下:

<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />

$('input:text:not(:eq(0))').hide()
$('input').on("change paste keyup", function () {
    if ($(this).val().length > 0) {
       $(this).next().show();
    }
else
{
    $(this).next().hide();
}
});

I have made sample application for same ,please click on given link for Demo 我已经做了相同的示例应用程序,请单击给定链接进行演示

See Demo application 请参阅演示应用程序

It's at Client side code so its performance is so fast rather than Server Side. 它是在客户端代码上,因此它的性能比服务器端要快。

Please vote me if you feel your problem is resolved by my idea. 如果您认为您的问题已被我的想法解决,请投票给我。

I'd name these text boxes similarly like "textbox1", "textbox2", "textbox3" so you can easily find the index of current text box. 我将这些文本框命名为类似“ textbox1”,“ textbox2”,“ textbox3”的名称,以便您可以轻松找到当前文本框的索引。 Then you can use KeyDown event to control what will be shown and what not. 然后,您可以使用KeyDown事件来控制显示什么和不显示什么。 This is not a working example but it should give you a good direction. 这不是一个可行的示例,但它应该为您提供良好的指导。

        int currentIndex = 1;

        private void TextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox t = Controls["TextBox" + (currentIndex + 1).ToString()] as TextBox;
            t.Visible = true;
            currentIndex +=1;
        }

You should use java-script for this because if you will use server side function for this then It will go to server so many times by this your application performance also will decrease. 您应该为此使用Java脚本,因为如果您要为此使用服务器端功能,那么它将由此进入服务器很多次,因此您的应用程序性能也会降低。

So create a java-script function that will accept one argument. 因此,创建一个将接受一个参数的Java脚本函数。 This argument will take next text box id (text box u want to display). 该参数将采用下一个文本框ID(您要显示的文本框)。 call this javascript function like this:- onkeyup="calgrid(this,"TextBox2");" 像这样调用此javascript函数:-onkeyup =“ calgrid(this,” TextBox2“);” pass nexttextbox id in place of TextBox2... 通过nexttextbox id代替TextBox2 ...

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

 function calgrid(firsttextbox,nexttextbox)
{

    var id=firsttextbox.id;
    var lastindex= id.lastIndexOf("_");
    var strclnt=id.slice(0,lastindex+1);
    var txtboxvalue=document.getElementById(firsttextbox).value;
if(txtboxvalue!="")
{
      document.getElementById(strclnt+nexttextbox).style.visibility='visible';
}
else
{
  document.getElementById(strclnt+nexttextbox).style.display = "none";
}


}
</script>

note:- If you will do visible=false from textbox property then we cannt do visible=true from javascript. 注意:-如果您将通过textbox属性执行visible = false,那么我们将无法通过javascript执行visible = true。 So Set style for all textbox style="display:none" 因此,为所有文本框设置样式style =“ display:none”

使用可以在第一个文本框中使用Keydown事件

try this code 试试这个代码

initially set flag=1 as first textbox is going to be by default visible 最初设置标志= 1,因为默认情况下第一个文本框将可见

private void visibleTextBox(Control c) { int flag = 1; 私有无效visibleTextBox(Control c){int flag = 1;

  foreach (Control c1 in c.Controls) { if (c1.GetType() == typeof(TextBox)) { if (flag == 1) { ((TextBox)c1).Visible = true; } else { ((TextBox)c1).Visible = false; } if (((TextBox)c1).Text != "") { flag = 1; } else { flag = 0; } } } } 

Comparatively simple solution in JavaScript. JavaScript中相对简单的解决方案。 The code should be somehow like this. 代码应该是这样的。

Define onchange event on text boxes like this: 在文本框中定义onchange事件,如下所示:

    <asp:TextBox ID="txt1" runat="server" onchange="show('txt1', 'txt2');"></asp:TextBox>
    <asp:TextBox ID="txt2" runat="server" onchange="show('txt2', 'txt3');" Style="visibility: hidden;"></asp:TextBox>

Then use this JavaScript code to show the next TextBox conditionally. 然后使用此JavaScript代码有条件地显示下一个TextBox。 Put this code in the head tag of the page: 将此代码放在页面的head标签中:

<script type="text/javascript">
    function show(txtCurrent, txtNext) {

        var valueCurrent = document.getElementById(txtCurrent).value;
        //alert(valueCurrent);

        if (valueCurrent.length > 0) {
            document.getElementById(txtNext).style.visibility = 'visible';
        }
        else {
            document.getElementById(txtNext).style.visibility = 'hidden';
        }

    }
</script>

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

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