Javascript 如何更改标签的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3584145/
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
How to change the text of a label?
提问by Amit
I have a radiobutton list and on click on the radio button item I have to change the text of its label. But for some reason it's not working. Code is below:
我有一个单选按钮列表,单击单选按钮项后,我必须更改其标签的文本。但由于某种原因它不起作用。代码如下:
<asp:Label ID="lblVessel" Text="Vessel:" runat="server"></asp:Label>
<script language="javascript">
$(document).ready(function() {
$('#rblDiv input').click(function() {
var selected = $("#rblDiv input:radio:checked").val();
if (selected == "exportpack") {
$('#lblVessel').text("NewText");
}
});
});
</script>
采纳答案by SLaks
ASP.Net automatically generates unique client IDs for server-side controls.
ASP.Net自动为服务器端控件生成唯一的客户端 ID。
Change it to
将其更改为
$('#<%= lblVessel.ClientID %>')
In ASP.Net 4.0, you could also set the ClientIDModepropertyto Staticinstead.
在ASP.Net 4.0,您还可以在设置ClientIDMode属性来Static代替。
回答by user1697483
I was having the same problem because i was using
我遇到了同样的问题,因为我正在使用
$("#LabelID").val("some value");
I learned that you can either use the provisional jquery method to clear it first then append:
我了解到您可以使用临时 jquery 方法先清除它,然后附加:
$("#LabelID").empty();
$("#LabelID").append("some Text");
Or conventionaly, you could use:
或者传统的,你可以使用:
$("#LabelID").text("some value");
OR
或者
$("#LabelID").html("some value");
回答by Codesleuth
Try this:
尝试这个:
$('[id$=lblVessel]').text("NewText");
The id$=will match the elements that end with that text, which is how ASP.NET auto-generates IDs. You can make it safer using span[id=$=lblVessel]but usually this isn't necessary.
在id$=将匹配的元素与文本,这是多么ASP.NET自动生成ID的结束。您可以使其更安全,span[id=$=lblVessel]但通常这不是必需的。
回答by DCG Scuff
try this
尝试这个
$("label").html(your value);or $("label").text(your value);
$("label").html(your value);或者 $("label").text(your value);
回答by Sravan Kumar
<asp:RadioButtonList ID="rbtnType" runat="server">
<asp:ListItem Value="C">Co</asp:ListItem>
<asp:ListItem Value="I">In</asp:ListItem>
<asp:ListItem Value="O">Out</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="lblLabelName" runat="server"></asp:Label>
<script type="text/javascript">
$(document).ready(function() {
$("#<%=rbtnType.ClientID%>").change(function() {
var rbvalue = $("input[@name=<%=rbtnType.ClientID%>]:radio:checked").val();
if (rbvalue == "C") {
$('#<%=lblLabelName.ClientID %>').html('text1');
} else if (rbvalue == "I") {
$('#<%=lblLabelName.ClientID %>').html('else text2');
} else if (rbvalue == "O") {
$('#<%=lblLabelName.ClientID %>').html('or elsethistext');
}
});
});
</script>
回答by atconway
I just went through this myself and found the solution. See an ASP.NET label server control actuallygets redered as a span (not an input), so using the .val() property to get/set will not work. Instead you must use the 'text' property on the span in conjuntion with using the controls .ClientID property. The following code will work:
我自己刚刚经历了这个并找到了解决方案。看到 ASP.NET 标签服务器控件实际上被重新命名为跨度(不是输入),因此使用 .val() 属性来获取/设置将不起作用。相反,您必须结合使用控件 .ClientID 属性使用跨度上的“文本”属性。以下代码将起作用:
$("#<%=lblVessel.ClientID %>").text('NewText');
回答by Vikas Kottari
$('#<%= lblVessel.ClientID %>').html('New Text');
ASP.net Label will be rendered as a span in the browser. so user 'html'.
ASP.net 标签将在浏览器中呈现为一个跨度。所以用户'html'。
回答by Tamilan
we have to find label tag for attribute value based on that.we have replace label text.
我们必须根据那个找到属性值的标签标签。我们已经替换了标签文本。
Script:
脚本:
<script type="text/javascript">
$(document).ready(function()
{
$("label[for*='test']").html("others");
});
</script>
Html
html
<label for="test_992918d5-a2f4-4962-b644-bd7294cbf2e6_FillInButton">others</label>
回答by suraj khot
lable value $('#lablel_id').html(value);

