【问题标题】:javascript Array from the request来自请求的 javascript 数组
【发布时间】:2023-03-27 17:35:01
【问题描述】:

enter code here我有一个来自服务器的变量,它定义了一个代表国家/地区的 Javascript ArrayList,如下所示:

[Belgium,Denmark,Germany,Italy,Portugal,Ireland,India,Morocco,Republic Of Korea];

在不带引号的jsp中从请求中获取变量:

var countries = ${countryList};

我收到此错误:

SCRIPT1007: Expected ']' 

使用引号没有错误

var countries = '${countryList}';

但结果不是预期的。我要访问

'Belgium' as countries[0] 
'Denmark' as countries[1] 

而不是 'B' as countries[0]'e' countries[1] 等...

【问题讨论】:

    标签: javascript html arrays jstl


    【解决方案1】:

    您正在混合服务器端和客户端。您需要使用 JSTL 遍历您的列表,以便构建您的 JavaScript 数组:

    var countries = [];
    <c:forEach items="${countryList}" var="country"> 
        countries.push('${country}');
    </c:forEach>
    

    【讨论】:

    • 为什么要在javascript中重建数组?为什么不在服务器上构建它并传递给 javascript?
    猜你喜欢
    • 2016-06-28
    • 2016-11-24
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    相关资源
    最近更新 更多