![](/img/trans.png)
[英]SET Parent page (aspx) label text from User Control (ascx) using JavaScript and without PostBack()
[英]set the value for aspx label element from javascript
我正在使用 javascript 调用 Web API,我有一个 ID 为“blbtest”的 aspx label 元素。
<asp:Label ID="blbtest" Text="" runat="server ClientIDMode="Static">
我想将 name 变量的值存储在此 label 中,但出现错误
这是js代码:
var request = new XMLHttpRequest;
request.open('GET', "https://hplussport.com/api/products?qty=2");
request.onload = function () {
var response = request.response;
var parsedData = JSON.parse(response);
console.log(parsedData);
var name = parsedData[0].name;
var products = document.createElement('li');
//products.innerHTML = name;
//document.body.appendChild(products);
var lbl = document.getElementById('<%=blbtest.ClientID%>');
lbl.innerText = name;
};
request.send();
错误:未捕获类型错误:无法在 XMLHttpRequest.request.onload (script.js:14:19) 处设置 null 的属性(设置“innerText”)
注释行有效。 但是我想在页面加载时在 label 元素中显示产品名称。
所以,我尝试了这个并且成功了:)
document.getElementById('blbtest').innerHTML = name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.