尝试从 while 循环中获取所有值以在 jquery 中使用。
这是我的循环:
<%
while (rs.next()) {
%>
<%=StringUtils.defaultString(rs.getString("program"), "")%><br />
<input type="hidden" id="program" name="program" value="<%=StringUtils.defaultString(rs.getString("program"), "")%>" />
<%
}
%>
应返回的值为 Art、Accounting。
这是我的 jQuery:
var progs = $('#program').map(function() {
return $(this).val();
}).get();
alert(progs); // alerts Art
alert(progs.length); // alerts 1
那么关于为什么这只返回第一个有什么想法吗?提前致谢
最佳答案
为什么这只返回第一个?因为 id 必须是唯一的,请使用类..
<%
while (rs.next()) {
%>
<%=StringUtils.defaultString(rs.getString("program"), "")%><br />
<input type="hidden" class="program" name="program" value="<%=StringUtils.defaultString(rs.getString("program"), "")%>" />
<%
}
%>
还有
var progs = $('.program').map(function() {
return $(this).val();
}).get();
alert(progs);
alert(progs.length);
Note: If you don't have control to change the
id
toclass
you can use$('[id^=program]')
selector instead of$('#program')
关于javascript - JQuery 从 while 循环中的输入获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44684080/