[英]read asp.net control in client side
I'm using the javascript below to read the value of an asp.net control in the client-side. 我正在使用下面的JavaScript在客户端读取asp.net控件的值。 However, it always returns the null value.
但是,它总是返回空值。 I'm using similar code at other pages in my website, but now i can't read this specific control.
我在网站的其他页面上使用了类似的代码,但是现在我无法阅读此特定控件。 Please suggest anyways I can fix this problem.
无论如何请提出建议,我可以解决此问题。
<asp:Label ID="srch_data" runat="server" ClientIDMode="Static" ></asp:Label>
var srch_data = document.getElementById("<%= srch_data.ClientID %>");
alert(srch_data);
尝试使用单引号:
var srch_data = document.getElementById('<%= srch_data.ClientID %>').value;
Try this 尝试这个
var srch_data =document.getElementById('srch_data').innerHTML;
ASP.Net Label becomes span after rendering so instead of finding a label which can not be recognized by JS better find ASP.Net标签在渲染后变为跨度,因此最好不要找到JS无法识别的标签
Add the defer attribute to your script element. 将defer属性添加到脚本元素。 I tested and it worked.
我进行了测试,并且有效。 Try something like below -
尝试以下类似的方法-
<%--defer indicates the script to be run after the document is completely parsed.--%>
<script type="text/javascript" language="javascript" defer="true">
var label = document.getElementById("<%= srch_data.ClientID %>");
alert("label : " + label);
</script>
<asp:Label ID="srch_data" runat="server" ClientIDMode="Static"></asp:Label>
This should fix your issue. 这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.