简体   繁体   English

如何使用JavaScript更改ASP标签中的值

[英]How to change value in asp Label using javascript

I have create a function onchange when there a textbox value change. 当文本框值发生更改时,我创建了一个onchange函数。 I able to show the result into label using innerHTML using Javascript. 我能够使用Java使用innerHTML将结果显示到标签中。 But when come to code behind, ASP.Net VB is not able to get the label.text value. 但是当涉及到代码背后时,ASP.Net VB无法获取label.text值。 Is there any way to show the result in this label? 有什么方法可以在此标签中显示结果吗?

<asp:Label ID="lblreserve1" runat="server" text="**HERE**" Visible="true"></asp:Label>
<input type="text" name="reservation-time" id="reservation-time" class="form-control" value="01/01/2016 - 01/25/2016" onchange="myFunction1(this.value)"/>
function myFunction1(reservedate) {
  var x = document.getElementById("reservation-time").value;
  document.getElementById("<% =lblreserve1.clientID %>").innerText = x;
}
MsgBox(lblreserve1.Text)

I think this is an XY problem. 我认为这是一个XY问题。 I don't think you want to change that label. 我认为您不想更改该标签。

I imagine you have a textbox that is serving double duty. 我想您有一个兼职的文本框。 If the label says one thing, the textbox contains data that means one thing; 如果标签说一件事,则文本框包含表示一件事的数据。 if the label says something else, the textbox contains some other data. 如果标签上有其他说明,则文本框包含其他数据。 This is a really unusual way to accomplish what is a very common web UX. 这是完成非常普通的Web UX的一种非常不寻常的方法。

Instead, create a series of label/textbox pairs and enclose them in DIV elements. 而是创建一系列标签/文本框对,并将其包含在DIV元素中。 Hide all DIVs but one of them. 隐藏除其中之一以外的所有DIV。 When you wish to "change" the label, unhide the DIV containing the label/textbox you want, and hide everything else. 当您希望“更改”标签时,请取消隐藏包含所需标签/文本框的DIV,然后隐藏其他所有内容。

When the form posts back to the server, all three textboxes will post back too. 当表单回发到服务器时,所有三个文本框也将回发。 The server can tell which label was shown because its textbox will contain a value. 服务器可以知道显示了哪个标签,因为其文本框将包含一个值。 Thus the server can infer the value of the label that was being shown when the form was submitted. 因此,服务器可以推断提交表单时显示的标签的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM