问题
how do i read an array from javascript to jinja template:
<script type="text/javascript">
var x =0;
function ActionDeterminator() {
x=x+1;
document.getElementById("me").innerHTML=x;
var $label = $('input[id = optionD]').next();
$label.text(x);
alert('{{option_D[0]}}');
return true;
}
</script>>
when i use the code above.. it works pretty well.but when i want to put the variable x like this:
<script type="text/javascript">
var x =0;
function ActionDeterminator() {
x=x+1;
document.getElementById("me").innerHTML=x;
var $label = $('input[id = optionD]').next();
$label.text(x);
alert('{{option_D[x]}}');
return true;
}
</script>>
i get an empty alert dialog. can anyone tell me hw to fix this pls
回答1:
You should be able to realize that in '{{option_D[x]}}'
, the entire expression is being evaluated by Jinja, before it ever gets to the browser, and Jinja knows nothing about what x
is.
Instead, you should get Jinja output the entire list as a JS array, and then get the Javascript to do the element selection:
alert({{ option_D }}[x]);
You will probably need to convert option_D
to JSON at the server side before sending it to the template for this to work.
来源:https://stackoverflow.com/questions/19927833/access-array-from-javascript-to-jinja