简体   繁体   English

ASPX JavaScript通过ID获取元素值

[英]ASPX JavaScript get element value by id

I'm trying to get value from ASPX element with JavaScript but getting 我正在尝试使用JavaScript从ASPX元素中获取价值,但是

Uncaught TypeError: Cannot read property 'value' of null Error . Uncaught TypeError: Cannot read property 'value' of null Error的Uncaught TypeError: Cannot read property 'value' of null

This is HTML markup: 这是HTML标记:

<tr>
    <td>Labour Cost:</td>
    <td><asp:Label ID="lbllabourCost" runat="server" /></td>
</tr>

And below is my JavaScript: 下面是我的JavaScript:

 <script>
    var labcost = document.getElementById('<%= lbllabourCost %>').value;
    console.log(labcost);
 </script>

What am I doing wrong here? 我在这里做错了什么?

Consider using the ClientID property, which will resolve the client-side id attribute present for that control : 考虑使用ClientID属性,该属性将解决该控件存在的客户端id属性:

var labcost = document.getElementById('<%= lbllabourCost.ClientID %>').innerHTML;

Additionally, you may want to consider which property that you are targeting within Javascript (ie using innerHTML or textContent since you are targeting a label). 另外,您可能要考虑在Javascript中定位的属性(即,由于定位标签而使用innerHTMLtextContent )。

Probably two things. 大概有两件事。

First the ID to get the element in DOM and then to get the contents. 首先是ID,以获取DOM中的元素,然后获取内容。

Try : 尝试:

var labcost = document.getElementById('<%= lbllabourCost.ClientID %>').textContent;

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

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