将数组从EJS传递到Javascript [英] passing an array from EJS to Javascript
本文介绍了将数组从EJS传递到Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将数组从ejs传递到JavaScript.我可以获取ejs中的值,但不能从JavaScript中获取值.我一直都不确定,因为变量"test"的内容是字符串而不是数组.
I am trying to passe an array from ejs to JavaScript. I can get to the values inside ejs but not from JavaScript. all the time i get undefined because the contents of the variable "test" is a string is not an array.
<script>
var test = '<%- level_tab %>';
alert(test);
function level(s1,s2){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
s2.innerHTML = "";
if(s1.value == "level_0"){
var optionArray = test;
}
else if(s1.value == "level_1"){
var optionArray = ["test|test01", "test0|test02"];
}
for(var option in optionArray){
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s2.options.add(newOption);
}
}
</script>
推荐答案
您必须对数组进行字符串化
You have to stringify the array
var test = <%- JSON.stringify(level_tab) %>;
我不熟悉EJS,但是即使EJS的语法略有不同,通常也应遵循相同的原理.
I'm not familiar with EJS but in general the same principle should apply even if syntax is slightly different in EJS.
这篇关于将数组从EJS传递到Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文