【发布时间】:2016-05-26 12:30:15
【问题描述】:
我在网格视图中有一个列表框,其中最后一项称为“其他”。我需要找到一种方法,以便在选择“其他”时显示一个文本框供用户输入一个值,如果取消选择,则该文本框将被隐藏。
我正在尝试在客户端执行此操作(使用 jquery 或 javascript)。
【问题讨论】:
标签: javascript jquery asp.net vb.net
我在网格视图中有一个列表框,其中最后一项称为“其他”。我需要找到一种方法,以便在选择“其他”时显示一个文本框供用户输入一个值,如果取消选择,则该文本框将被隐藏。
我正在尝试在客户端执行此操作(使用 jquery 或 javascript)。
【问题讨论】:
标签: javascript jquery asp.net vb.net
你可以试试:
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 控件。
【讨论】:
经过大量测试后,我使用直接 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 兼容。 我还必须根据数据库中的值为自己计算出文本框的初始状态。
【讨论】: