-6ren">
gpt4 book ai didi

java - 从服务器端获取值并分配给 JavaScript 变量

转载 作者:行者123 更新时间:2023-12-02 06:44:06 24 4
gpt4 key购买 nike

我在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/

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