<%
Map map = new HashMap();
map.put("20",0);
map.put("21",1);
request.setAttribute("map",map);
%>
js中不能直接如下取值
var key = "21";
alert("${map[" + key + "]}");//想直接实现这种取值效果,不行
下面通过转成json来实现
<script type="text/javascript">
String.prototype.replaceAll = function(s1,s2) {
return this.replace(new RegExp(s1,"gm"),s2);
}
var str = "${map}";
str = str.replaceAll("=",":");
var map = eval("(" + str + ")");
alert(map[key]);
</script>
Map map = new HashMap();
map.put("20",0);
map.put("21",1);
request.setAttribute("map",map);
%>
js中不能直接如下取值
var key = "21";
alert("${map[" + key + "]}");//想直接实现这种取值效果,不行
下面通过转成json来实现
<script type="text/javascript">
String.prototype.replaceAll = function(s1,s2) {
return this.replace(new RegExp(s1,"gm"),s2);
}
var str = "${map}";
str = str.replaceAll("=",":");
var map = eval("(" + str + ")");
alert(map[key]);
</script>