如何使用 Javascript 获取 ASP.NET Web Forms 标签的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7431464/
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 use Javascript to get ASP.NEt Web Forms label's value?
提问by CiccioMiami
I have the following label control:
我有以下标签控件:
<asp:Label ForeColor="DarkGreen" runat="server" ID="lblStatus"></asp:Label>
Its value is filled in the Page_Loadevent.
它的值填充在Page_Load事件中。
I attached the following Javascript (placed at the end of the page, not Master page):
我附上了以下 Javascript(放在页面的末尾,而不是母版页):
function Validate() {
var lblObj = document.getElementById('<%=lblStatus.ClientID%>');
alert(lblObj.value);
if (lblObj.value == "Replaced" || lblObj.value == 'Trashed' || lblObj.value == "Internal Use") {
alert("Products with" + lblObj.value + "status cannot be reserved");
return false;
}
}
The alert(lblObj.value)displays a popup with text "undefined". How can I fix this problem? Please, I tried many combinations for placing the JavaScript but no luck! Thanks
该警报(lblObj.value)显示与文本弹出“不确定”。我该如何解决这个问题?拜托,我尝试了很多组合来放置 JavaScript,但没有运气!谢谢
UPDATE
更新
Browser soruce code:
浏览器源代码:
<span id="ctl00__main_lblStatus" style="color:DarkGreen;">Available</span></td>
First line of Validate JS function:
验证 JS 函数的第一行:
function Validate() {
var lblObj = document.getElementById('ctl00__main_lblStatus');
回答by Daniel A. White
label
s don't have a value
. They have innerHTML
and innerText
.
label
s 没有value
. 他们有innerHTML
和innerText
。
回答by Canavar
Label server control renders as span. So you should get it's content by innerText. try this :
标签服务器控件呈现为跨度。所以你应该通过innerText获取它的内容。试试这个 :
alert(lblObj.innerText);
回答by Davide Piras
Use JQuery and it will run in all browsers and platforms, something like:
使用 JQuery,它将在所有浏览器和平台上运行,例如:
$('#<%= lblStatus.ClientID %>').next().text();
source: JQuery: getting the value/text/innerHtml of a checkbox in an ASP.NET CheckBoxList control
回答by slobodans
ASP.NET label server control will be rendered in complex HTML output. Like:
ASP.NET 标签服务器控件将呈现在复杂的 HTML 输出中。喜欢:
<span id="ctl00_ctl00_ContentPlaceHolder1_BodyPlaceHolder_lblLanguage0">
<label class="inputText">English</label>
</span>
When you use getElementById you will get span. But to set value via javascript you have to access inner label object
当您使用 getElementById 时,您将获得跨度。但是要通过 javascript 设置值,您必须访问内部标签对象
回答by raje
try this
试试这个
<script language="javascript" type="text/javascript">
function getlabelvalue()
{
var value1 = document.getElementById('<%=labelID.ClientID%>').value;
if (value1.length < 1)
value1 = 0;
}
</script>
回答by Enkode
With jquery you need to use the html method.
使用 jquery,您需要使用 html 方法。
var g = $('#<%=lblStatus.ClientID%>').html();
These will NOT work with jquery:
这些不适用于 jquery:
- $('#<%=lblStatus.ClientID%>').innerText
- $('#<%=lblStatus.ClientID%>').innerHTML
- $('#<%=lblStatus.ClientID%>').val()
- $('#<%=lblStatus.ClientID%>').innerText
- $('#<%=lblStatus.ClientID%>').innerHTML
- $('#<%=lblStatus.ClientID%>').val()