【发布时间】:2019-07-23 11:07:36
【问题描述】:
如下所示,我有一个循环,在其中创建一些 ASP 标签。
function func(val) {
var mod = Math.ceil(val / 9);
for (var i = 0; i < mod; i++) {
html = '<div class="carousel-item">
<table>
<tr>
<td>Temp ' + parseInt(i * 9 + 1) + '</td>
<td>Temp ' + parseInt(i * 9 + 2) + '</td>
<td>Temp ' + parseInt(i * 9 + 3) + '</td>
</tr>
<tr>
<td><asp:Label ID="TempCL1" runat="server" Text="xxx"></asp:Label></td>
<td><asp:Label ID="TempCL2" runat="server" Text="xxx"></asp:Label></td>
<td><asp:Label ID="TempCL3" runat="server" Text="xxx"></asp:Label></td>
</tr>
</table>
</div>';
$(".carousel-inner").html($(".carousel-inner").html() + html);
}
}
我的代码工作正常,但我需要修改标签 ID。例如: 来自
'<tr><td class="Valorppal"><asp:Label ID="TempCL1" runat="server" Text="xxx"></asp:Label></td>'
类似
'<tr><td class="Valorppal"><asp:Label ID="TempCL' + parseInt(i * 9 + 1) + '" runat="server" Text="xxx"></asp:Label></td>'
但我收到一个错误(“TempCL' + parseInt(i * 9 + 1) + '” 不是有效标识符。)
【问题讨论】:
-
当心:您正试图在在浏览器内生成
<asp:Label ID="..." runat="server">标签,但这永远不会起作用:带有runat="server"的标签只会做你想做的事,如果他们从您的服务器端 ASP.NET 代码内部创建或生成,因为只有这样 IIS 和 .NET 才能为您执行它们。浏览器根本不知道<asp:Label>是什么。 -
@PeterB 嗯..但是我的第一段代码无论如何都可以工作......我明白......那么我应该怎么做才能得到我想要的?
-
首先看这个:Create multiple labels on fly in asp.net。更一般地说,您可能会从查看几个 ASP.NET WebForms 教程(例如在 YouTube 或 Microsoft Virtual Academy 上搜索)中受益。
-
但我不能从服务器端做到这一点......我需要它来自 Javascript。我尝试使用替换函数来替换 ID 字符串,但它也不起作用
-
您不能使用 javascript 设置服务器控制 ID。正如@PeterB 所说,您必须动态创建它们。但你的问题听起来像是X-Y problem。
标签: javascript jquery html asp.net