Javascript 如何更改javascript函数中的标签文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8200729/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:04:47  来源:igfitidea点击:

How to change the label text in javascript function?

javascriptasp.net

提问by user1047651

i want to validate login page using java script function. i take one label button for displaying message and two text box for username and password and one button for "Login" .

我想使用 java 脚本函数验证登录页面。我使用一个标签按钮来显示消息,两个文本框用于用户名和密码,一个按钮用于“登录”。

when user click on "Login" button without enterning username and password then message will be display in label control. So how to check textbox value in javascript function and how to change the value in lblmessage. I call this function in Ok button . I set this function on Button OnClientClick event but this does not work.

当用户在没有输入用户名和密码的情况下单击“登录”按钮时,将在标签控件中显示消息。那么如何检查 javascript 函数中的文本框值以及如何更改 lblmessage 中的值。我在 Ok button 中调用了这个函数。我在 Button OnClientClick 事件上设置了这个函数,但这不起作用。

回答by Starx

With javascript

使用 JavaScript

A simple example

一个简单的例子

var lblElement = document.getElementById("yourlabelsid");

lblElement.innerHtml("Your new updated text in the label");

But try to use a javascript libray like jquery, it has a lot of benefits and ease. Generically on jquery, to change the markup of any element, we use

但是尝试使用像 jquery 这样的 javascript 库,它有很多好处和易用性。通常在 jquery 上,要更改任何元素的标记,我们使用

$("#theselector").html("the new markup inside the markup");

In case input elements, we use .val(); $("#mytextbox").val("the value in the textbox");

对于输入元素,我们使用 .val(); $("#mytextbox").val("文本框中的值");



Based on your description, you want to change the text inside label so a simple example

根据您的描述,您想更改标签内的文本,这是一个简单的示例

For this label

对于这个标签

<label id="lbl_name">Name:</label>

Use

$("#lbl_name").html("the updated html"); 

Anywhere you need on the script

脚本上任何您需要的地方

回答by Samir Adel

It is better to use the asp.net required field validator control instead of making a special javascript function to handle this .. it is simple as setting the control to validate property of the required field validator to the id of the textbox

最好使用 asp.net 必需字段验证器控件,而不是制作特殊的 javascript 函数来处理此问题.. 将控件设置为验证必需字段验证器的属性为文本框的 id 很简单

回答by user2125523

var msgLabel= document.getElementById('<%=lblMessage.ClientID %>');
msgLabel.innerHTML = "Your message here";

var msgLabel= document.getElementById('<%=lblMessage.ClientID %>');
msgLabel.innerHTML = "您的信息在这里";

回答by Enigma State

<script language="javascript">

    function check() {
     var a = document.getElementById('<%=label.ClientID%>');
     var b = document.getElementById('<%=textbox.ClientID%>').Value;
     var c = document.getElementById('<%=textbxpassword.ClientID%>').Value;

        if (b  == "" &&  c == "") 
        {
             a.innerHTML = "your text here";
             alert(a.innerHTML);
        }
     }

</script>