作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
-6ren">
我在request
中有字符串数组值对象,我像下面这样检索它,
<%
if (request.getAttribute("categoriesList") != null) {%>
<input id="categoriesList" name="categoriesList" type="hidden" value="<%= (String[]) request.getAttribute("categoriesList") %>"/>
<c:forEach var="categoryName" items="${categoriesList}">
<li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
</c:forEach>
<% }%>
这会将值打印为 <li>
但我也有隐藏变量,我想为其分配值,
<input id="categoriesList" name="categoriesList" type="hidden" value="<%= (String[]) request.getAttribute("categoriesList") %>"/>
但是当我使用如下所示的 java 脚本打印它们时,
var category= new Array();
category = $('#categoriesList').val();
console.log(category);
for (var i in category) {
console.log(category[i]);
}
我明白了,
[Ljava.lang.String;@f2bc8d rechargeWalletRequest.htm:182
[ rechargeWalletRequest.htm:186
L rechargeWalletRequest.htm:186
j rechargeWalletRequest.htm:186
a rechargeWalletRequest.htm:186
v rechargeWalletRequest.htm:186
a rechargeWalletRequest.htm:186
. rechargeWalletRequest.htm:186
l rechargeWalletRequest.htm:186
a rechargeWalletRequest.htm:186
n rechargeWalletRequest.htm:186
g rechargeWalletRequest.htm:186
. rechargeWalletRequest.htm:186
S rechargeWalletRequest.htm:186
t rechargeWalletRequest.htm:186
r rechargeWalletRequest.htm:186
i rechargeWalletRequest.htm:186
n rechargeWalletRequest.htm:186
g rechargeWalletRequest.htm:186
; rechargeWalletRequest.htm:186
@ rechargeWalletRequest.htm:186
f rechargeWalletRequest.htm:186
2 rechargeWalletRequest.htm:186
b rechargeWalletRequest.htm:186
c rechargeWalletRequest.htm:186
8 rechargeWalletRequest.htm:186
d rechargeWalletRequest.htm:186
我应该获得该值,但它打印了一些其他内容。
请帮助完成此任务。
最佳答案
您必须为每个输入隐藏语句分配一个唯一的 ID。像这样的事情:
<%
if (request.getAttribute("categoriesList") != null) {%>
<c:forEach var="categoryName" items="${categoriesList}">
<input id="${categoryName}" name="categoriesList" type="hidden" value="${categoryName}"/>
<li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
</c:forEach>
<% }%>
然后在 jQuery 中获取所有具有 name 属性的元素,并迭代它们以获取值。
关于java - 从服务器端获取值并分配给 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822867/
我是一名优秀的程序员,十分优秀!