Jslfl【软件开发技术笔记】

js取el中的map值及js replaceAll实现

1
2
3
4
5
6
<%
Map map = new HashMap();
map.put("20",0);
map.put("21",1);
request.setAttribute("map",map);
%>

js中不能直接如下取值

1
2
var key = "21";
alert("${map[" + key + "]}");//想直接实现这种取值效果,不行

下面通过转成json来实现

1
2
3
4
5
6
7
8
9
10
11
<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>

, , ,

Comments are currently closed.