Access Array from javascript to jinja

百般思念 提交于 2019-12-13 07:56:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!