繁体   English   中英

选择特定的列表框项目时如何显示文本框?

[英]How to show a textbox when a specific listbox item is selected?

我在gridview中有一个列表框,其中最后一项称为“其他”。 我需要找到一种方法,以便在选择“其他”时显示一个文本框供用户键入一个值,如果取消选择该文本框,则该文本框将被隐藏。

我正在尝试在客户端(使用jquery或javascript)上执行此操作。

您可以尝试:

 Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim str As String
        str = ListBox1.SelectedItem.ToString
        If str.Equals("other") Then
            TextBox1.Visible = True
        Else
            TextBox1.Visible = False
        End If

    End Sub

但是,如果您不能直接关联该事件,则可以在load事件中创建关联:

For Each row As GridViewRow In gvProcesos.Rows     
   Dim ListBox1 As ListBox = row.FindControl("MylistBox")  
   AddHandler  ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
Next

在客户端,您可以尝试这些

   $('#<%=gdRows.ClientID %>').find('span[id$="lblID"]')

但用span代替呈现的HTML控件。

经过大量测试,我使用纯正的javascript作为解决方案

我的服务器代码

oListBox.Attributes.Add("onchange", "ShowHideTextbox('" & oListBox.ClientID & "','" & oTextbox.ClientID & "','" & oLabel.ClientID & "');")

我的JavaScript函数

    function ShowHideTextbox(oDropID, oTextID, oLabelID) {
    var ddl = document.getElementById(oDropID);
    var oTextbox = document.getElementById(oTextID);
    var oLabel = document.getElementById(oLabelID);
    var bShow = false;
    for (i = 0; i < ddl.length; i++) {
        if (ddl[i].selected) {
            if (ddl[i].text == "Other") bShow = true;
        }
    }
    if (bShow) {
        oLabel.style.display = 'inline';
        oTextbox.style.display = 'inline';
        oTextbox.focus();
    } else {
        oLabel.style.display = 'none'; 
        oTextbox.style.display = 'none'; 
    }
}

就我而言,我有一个标签和文本框正在显示/隐藏,我不确定它是否与Firefox兼容。 我还必须根据数据库中的值为自己确定文本框的初始状态。

暂无
暂无

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

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