Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在客户端读取asp.net控件_Javascript_Asp.net_C# 4.0 - Fatal编程技术网

Javascript 在客户端读取asp.net控件

Javascript 在客户端读取asp.net控件,javascript,asp.net,c#-4.0,Javascript,Asp.net,C# 4.0,我正在使用下面的javascript读取客户端中asp.net控件的值。但是,它总是返回空值。我在我的网站的其他页面上使用了类似的代码,但现在我无法阅读这个特定的控件。请建议我如何解决这个问题 <asp:Label ID="srch_data" runat="server" ClientIDMode="Static" ></asp:Label> var srch_data = document.getElementById("<%= srch_data.Clien

我正在使用下面的javascript读取客户端中asp.net控件的值。但是,它总是返回空值。我在我的网站的其他页面上使用了类似的代码,但现在我无法阅读这个特定的控件。请建议我如何解决这个问题

<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_数据);

尝试使用单引号:

var srch_data = document.getElementById('<%= srch_data.ClientID %>').value;
var srch_data=document.getElementById(“”).value;
试试这个

var srch_data =document.getElementById('srch_data').innerHTML;

ASP.Net标签在渲染后变为span,因此与其查找JS无法识别的标签,不如查找

将defer属性添加到脚本元素中。我测试了一下,它成功了。 试试下面的方法-

<%--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>

var label=document.getElementById(“”);
警报(“标签:”+标签);

这将解决您的问题。

所以srch_数据为空?你提到价值。。。但是标签没有值当ClientID设置为Static document.getElementById('srch_data').innerHTML时,不需要使用srch_data.ClientID