javascript 中不能使用visible=false 的ASP.NET 控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1996600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET control with visible=false cannot be used in javascript?
提问by Nevin Mathai
I have an ASP.NET text control "FromDate" whose visible property is set to false, but I wanted a client side javascript to be able to toggle the visibility property using CSS properties
我有一个 ASP.NET 文本控件“FromDate”,其可见属性设置为 false,但我希望客户端 javascript 能够使用 CSS 属性切换可见性属性
element1.style.display = "none"; // hides the element
element1.style.display = ""; // shows the element
but when I attempt to get the textbox, I get null on
但是当我尝试获取文本框时,我得到了空值
var element1 = document.getElementById("FromDate");
When I try the same code with visble=true as the default on the "FromDate" ASP.NET control, it works (although that is not the behavior I need)
当我尝试使用 visble=true 作为“FromDate”ASP.NET 控件上的默认值的相同代码时,它可以工作(尽管这不是我需要的行为)
Any ideas?
有任何想法吗?
回答by Gabriel McAdams
When you set Visible = false to a control, it is not rendered. That means there is no HTML representation of that control sent to the page. Set the style only.
当您将 Visible = false 设置为控件时,它不会被呈现。这意味着发送到页面的控件没有 HTML 表示。仅设置样式。
You can set the style as display: none from server side code like this:
您可以将样式设置为 display: none 来自服务器端代码,如下所示:
FromDate.Style.Add(HtmlTextWriterStyle.Display, "none")
回答by Allen Le
If you want to hide this control, you can try CSS like this:
如果你想隐藏这个控件,你可以像这样尝试 CSS:
<asp:somecontrol id="FromDate" style="display:none" />
I think hiding the control with CSS is easier to understand.
我认为用 CSS 隐藏控件更容易理解。
回答by Aviad P.
Instead of setting Visible=false, set its style.display to none, that way the element is still there for JavaScript to manipulate.
不是设置 Visible=false,而是将它的 style.display 设置为 none,这样元素仍然可以被 JavaScript 操作。

