在 JavaScript 中隐藏文本框
hide a textbox in javascript
尝试使用以下脚本隐藏文本框:
function EnableTextBox(clientId2, clientId1) {
var label = eval("document.getElementById('" + clientId2 + "')");
var textBox = eval("document.getElementById('" + clientId1 + "')");
if (label.Visible == true) {
label.Visible = false;
textBox.Visible = true;
}
else {
label.Visible = true;
textBox.Visible = false;
}
}
文本框与标签位于同一单元格中,事件在gridview_ondatabound事件期间在代码隐藏中创建:
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblNotes = (Label)(e.Row.Cells[1].Controls[1]);
TextBox tbNotes = (TextBox)(e.Row.Cells[1].Controls[3]);
if (lblNotes != null)
{
lblNotes.Attributes.Add("methodstring", string.Format("EnableTextBox('{0}', '{1}')", lblNotes.ClientID, tbNotes.ClientID));
lblNotes.Attributes.Add("onClick", "eval(this.methodstring)");
}
}
我还没有解决的问题是我的脚本中的变量 tbNotes 仍处于空状态。 有什么建议吗?
谢谢
你几乎在那里,你只需要使用style.display
来显示/隐藏元素。
在下面的代码中,将显示一个文本框,如果单击标签,标签将被隐藏。
按照你的逻辑,一旦标签被隐藏,我不知道如何显示它。
.ASPX
<asp:GridView runat="server" ID="GridView1"
AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="NotesLabel"
Text='<%# Eval("Notes") %>' />
<asp:TextBox runat="server" ID="NotesTextBox"
Text='<%# Eval("Notes") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript">
function enableTextBox(label, textbox) {
var lbl = document.getElementById(label);
var txt = document.getElementById(textbox);
if (lbl.style.display == "" || lbl.style.display == "block") {
lbl.style.display = "none";
txt.style.display = "block";
} else {
lbl.style.display = "block";
txt.style.display = "none";
}
}
</script>
代码隐藏
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = new List<Something>
{
new Something {Id = 1, Notes = "One"},
new Something {Id = 2, Notes = "Two"},
};
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var notesLabel = e.Row.FindControl("NotesLabel") as Label;
var notesTextBox = e.Row.FindControl("NotesTextBox") as TextBox;
notesLabel.Attributes.Add("onclick",
string.Format("enableTextBox('{0}', '{1}')",
notesLabel.ClientID, notesTextBox.ClientID));
}
}
public class Something
{
public string Notes { get; set; }
public int Id { get; set; }
}
注意:jQuery可能会容易得多,但它超出了范围。
使用e.Row.FindControl("ID_OF_CONTROL")
而不是尝试浏览子集合。
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblNotes = e.Row.FindControl("lblNotes") AS Label; // Proper Id of the control
TextBox tbNotes = e.Row.FindControl("tbNotes ") AS TextBox; // Proper Id of the control
if (lblNotes != null && tbNotes !=null)
{
lblNotes.Attributes.Add("methodstring", string.Format("EnableTextBox('{0}', '{1}')", lblNotes.ClientID, tbNotes.ClientID));
lblNotes.Attributes.Add("onClick", "eval(this.methodstring)");
}
}
相关文章:
- 在mvc应用程序中,在回发时保留最初隐藏的文本框的隐藏或可见状态
- 如何使用javascript隐藏文本
- 在显示/隐藏中单击时删除的文本
- 通过ng-if设置隐藏文本框的值
- Codemirror:将特定的基于模式的文本隐藏到编辑器中
- 使文本在它之后再次可见'It’s被一个按钮隐藏了
- 移动HTML输入文本框在提交时隐藏软键盘
- 只隐藏那些为空或没有文本的跨度
- Selenium认为文本框是隐藏的,即使我可以在浏览器中看到它
- 如何根据文本长度立即显示和隐藏字段?-JQuery
- Jquery日期选择器-我们如何将文本隐藏在日期选择器文本框中
- 使用 JS 将文本隐藏在元素中(而不是元素本身)
- 如何使一个文本隐藏另一个文本
- JQuery显示隐藏与无序列表:需要添加动态文本/隐藏功能
- 基于文本隐藏面板
- Span文本隐藏的动画DIV尽管z索引
- 为什么幻灯片面板打开时标题文本隐藏
- Javascript:切换纯文本隐藏它像密码文本点击按钮
- 当到达底部页面部分时,Jquery用锚文本隐藏Div
- Javascript,使多个文本隐藏文本输入可见