更改标签的值

时间:2018-09-07 08:08:14

标签: javascript asp.net button

我想使用带有Javascript的事件从Label更改值。在我的按钮上,我有一个事件onclick,当您单击它时,将转到especific函数,但我无法让他做我想做的事情。例如,启用或禁用元素。

我尝试使用普通按钮和asp按钮,但是没有用。 “标签可见”属性有效,因为标签消失了,但是当我按下按钮时它没有出现。

这是代码:

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        function myFunction() {
            var estado = document.getElementById('Label1');
            estado.Visible = true;
        }
    </script>
    <div id = "bloque1">
        <asp:Label ID="Label1" runat="server" Text="Label" Font-Size="55pt" Visible="false" />
    </div>
    <div class = "botones">
        <button onclick="myFunction()">Click me</button>
        <asp:Button ID="Button1" onclick="myFunction()" runat="server" Text="Button" />
    </div>
</asp:Content>

1 个答案:

答案 0 :(得分:0)

您的元素ID错误,应该为ctl00_MainContent_Label1(或类似名称)。 您真正应该做的是使用以下方法始终获取正确的ID:

var estado = document.getElementById('<%= Label1.ClientId %>');

这是因为ASP的ID元素与客户端上的ID元素不同。

第二,您要修改元素的style属性。

function myFunction() {
    var estado = document.getElementById('<%= Label1.ClientId %>');
    estado.style.visibility = "visible"; // use hidden if you want to hide it again.
}

最后;设置Visible="false"意味着Label将不会在页面上呈现...您应该使用样式属性代替style="visibility:hidden"

<asp:Label ID="Label1" runat="server" Text="Label" Font-Size="55pt" Visible="true" style="visibility:hidden;"></asp:Label>