jQuery 可以在asp.net mvc4 中使用jquery 在@html.label 中设置值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20469199/
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
Possible to set value in @html.label using jquery in asp.net mvc4?
提问by Joseph
i have a @Html.Label("", new { id="txtStatus1" })
我有一个 @Html.Label("", new { id="txtStatus1" })
where txtstatus1 value is obtained using jquery as $('#txtStatus1').val(TicketStatus);
其中 txtstatus1 值是使用 jquery 获得的 $('#txtStatus1').val(TicketStatus);
but im not able to set this value to label .
但我无法将此值设置为 label 。
回答by hutchonoid
This statement does not output anything as you have not specified a for value:
此语句不会输出任何内容,因为您尚未指定 for 值:
@Html.Label("", new { id="txtStatus1" })
If you change it to give it a value i.e.
如果你改变它给它一个值,即
@Html.Label("a", new { id="txtStatus1" })
It outputs this:
它输出这个:
<label for="a" id="txtStatus1">a</label>
Sridhar R is correct you can use text to set it like this:
Sridhar R 是正确的,您可以使用文本将其设置为:
$('#txtStatus1').text('this')
You might need to add quotes and output around the argument if it is comming from your model i.e.
如果它来自您的模型,您可能需要在参数周围添加引号和输出,即
$('#txtStatus1').val('@Model.TicketStatus');
What is TicketStatus exactly?
TicketStatus 究竟是什么?
回答by Sridhar R
Try this
尝试这个
Use .text()
or .html()
使用.text()
或.html()
Get html label value
获取html标签值
var txt = $('#lbltxt').html();
Set html label value
设置html标签值
$('#lbltxt').html("your value");
To get asp.net label value we need to write the code like as shown below
要获取asp.net标签值,我们需要编写如下所示的代码
var txt = $('#<%=lbltxt.ClientID%>').html();
or
或者
var txt = $("[id$='lbltxt']").html()
Set Asp.net label Value
设置 Asp.net 标签值
$('#<%= lbltxt.ClientID%>').html('Your Value')
Or
或者
$("[id$=' lbltxt']").html('Your Value')