【问题标题】:How to show a textbox when a specific listbox item is selected?选择特定列表框项目时如何显示文本框?
【发布时间】:2016-05-26 12:30:15
【问题描述】:

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

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

【问题讨论】:

    标签: javascript jquery asp.net vb.net


    【解决方案1】:

    你可以试试:

     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
    

    但如果您不能直接关联事件,您将在加载事件中创建关联:

    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 控件。

    【讨论】:

    • 对,我明白你的意思。那可能是服务器端解决方案。我正在寻找可能使用 jquery 的客户端。
    • 这不是服务器端解决方案。顺便说一句,您可能需要在第 3 行使用“str = ListBox1.SelectedItem(1).ToString”。据我记得,SelectedItem 是一个数组,其中“0”是 ValueMember,“1”是 DisplayMember。
    【解决方案2】:

    经过大量测试后,我使用直接 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 兼容。 我还必须根据数据库中的值为自己计算出文本框的初始状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      相关资源
      最近更新 更多