读取每个 id 各自的值时, var data = $("#attribute_1"-6ren">
gpt4 book ai didi

javascript - 在javascript中使用变量作为 'id'属性

转载 作者:行者123 更新时间:2023-12-03 02:00:01 25 4
gpt4 key购买 nike

我有以下 UI 的锯齿状代码,我通过 UI 从用户获取值。

<%
for (i = 0; i < applicationAttributes.length; i++) {
%>
<div>
<label>data:</label>
<div>
<input type="text" id = "attribute_<%= i%>" >
</div>
</div>
<%
}
%>

读取每个 id 各自的值时,

var data = $("#attribute_1").val();

但它没有返回值,有人可以指定分配“id”的正确方法

最佳答案

id 与其值之间不应有空格

<input type="text" id = "attribute_<%= i%>" >
<!- _________________^_^ -->

应该是

<input type="text" id="attribute_<%= i%>" />

使用 / 关闭 void 元素是一个很好的做法。

关于javascript - 在javascript中使用变量作为 'id'属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50077767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com