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

时间:2023-01-19 10:40:50

I have a listbox in a gridview where last item is called "other". I need to find a way so that when "other" is selected a textbox is shown for the user to type in a value, and if it is deselected then the textbox will be hidden.

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

I'm trying to do this on the client-side (with jquery or javascript).

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

2 个解决方案

#1


0  

You could try with:

你可以尝试:

 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

But if you can't associate directly the event you would create the association in the load event:

但是,如果您无法直接关联事件,则会在load事件中创建关联:

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

An in the client side you could try these

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

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

but substitute span for the rendered HTML control.

但是将span替换为呈现的HTML控件。

#2


0  

after much testing, I got to this as my solution using straight javascript

经过多次测试后,我使用直接的javascript作为我的解决方案

my server code

我的服务器代码

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

my javascript function

我的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'; 
    }
}

in my case I have a label and textbox that I am showing / hiding I am not sure yet if it is firefox compatible. I also have to work out for myself an initial state of the textbox depending upon values from database.

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

#1


0  

You could try with:

你可以尝试:

 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

But if you can't associate directly the event you would create the association in the load event:

但是,如果您无法直接关联事件,则会在load事件中创建关联:

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

An in the client side you could try these

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

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

but substitute span for the rendered HTML control.

但是将span替换为呈现的HTML控件。

#2


0  

after much testing, I got to this as my solution using straight javascript

经过多次测试后,我使用直接的javascript作为我的解决方案

my server code

我的服务器代码

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

my javascript function

我的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'; 
    }
}

in my case I have a label and textbox that I am showing / hiding I am not sure yet if it is firefox compatible. I also have to work out for myself an initial state of the textbox depending upon values from database.

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