【问题标题】:JavaScript don't hide text area in asp.netJavaScript 不会在 asp.net 中隐藏文本区域
【发布时间】:2012-06-20 11:32:26
【问题描述】:

我有一个奇怪的 Javascript 问题,我正在尝试实现系统来更新一些东西,当我按下按钮时,我想隐藏除“MainContent_UpdateProgress”之外的所有内容,但文本区域仍然可见。

asp.net 中的文本区域如下所示:

<textarea runat="server" id="serverOutputTextArea" cols="50" rows="30" name="serverOutputTextArea" visible="false">

Javascript 代码:

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
var postBackElement;
function InitializeRequest(sender, args) {
    if (prm.get_isInAsyncPostBack())
        args.set_cancel(true);
    postBackElement = args.get_postBackElement();

    if (postBackElement.id == 'MainContent_UpdateAnalysisSystem') 
    {
        $get('MainContent_serverOutputTextArea').style.display = 'none'; //Doesn't work

        $get('MainContent_UpdateProgress').style.display = 'block'; //Works
        $get('MainContent_ProcessingStatus_Label').style.display = 'none'; //Works
        $get('MainContent_ShowDetails_Button').style.display = 'none';  //Works
    }
}

问题是,textarea 有什么不同?

【问题讨论】:

  • 文本区域“MainContent_UpdateAnalysisSystem”的客户端 ID 是否没有得到类似“ctl00_ContentPlaceHolder1_MainContent_UpdateAnalysisSystem”的内容

标签: javascript asp.net textarea visibility


【解决方案1】:

您是否查看过浏览器的源代码以了解 textarea 的实际 id 是什么?可能是您将它放置在某个其他元素中,并且它继承了更长的 id。

否则,您可以通过以下方式获取 id:&lt;%= serverOutputTextArea.ClientID%&gt;

javascript 行如下所示:

$get('<%= serverOutputTextArea.ClientID %>').style.display = 'none';

【讨论】:

  • 我注意到@Fabio-Milheiro 更新了答案,他是对的。您遇到的问题是您是具有runat="server" 的控件上的用户visible="false"。这意味着该控件永远不会呈现,并且 javascript 无法找到它。如果您将 visible="true" 更改为 style="display: none;" 它应该可以工作。
【解决方案2】:

确认一下:MainContent_serverOutputTextArea是textarea的id吗?

如果是,你可以去 Firebug 并尝试将显示样式属性设置为无吗?或者出于绝望尝试 visibility=hidden。

但最终,您需要确保 $get 在 DOM 中选择正确的元素。

更新

嗯……我想我知道发生了什么。你有:

<textarea runat="server" id="serverOutputTextArea" ... visible="false">

您将 runat="server" 添加到 textarea,因此它将成为一个服务器控件,如果 Visible 属性为 false,它根本不会呈现到 HTML。

你知道这是什么意思吗?如果它未呈现,您的 JavaScript 将无法看到并选择它。将 Visible 属性设置为 true 即可。

【讨论】:

  • 将可见更改为 true 但仍然相同。它在我更改 CSS 时有效。但是如何切换 CSS?因为我需要稍后显示。
  • 如果你想切换 CSS,我建议你使用 JQuery。你可以找到它here。像这样设置它&lt;script src="http://docs.jquery.com/Downloading_jQuery"&gt;&lt;/script&gt;,然后你像这样简单地调用toggle函数$(#'&lt;%= serverOutputTextArea.ClientID %&gt;').toggle()
  • @Full_int,确认 textarea id 是你要找的那个吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2012-07-21
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多