简体   繁体   中英

How to work with django template variable and javascript?

I have a django template variable {% clients_list %}
I want to load this in multiple select boxes with same prefixes.
I am writing this code:

$(document).ready(function(){
    for (i=1; i<=30; i++){
        for(j=0; j<=clients_list.length - 1; j++){
            $('#client'+i).append($('<option>')
            .text(clients_list[j])
            .attr('value', clients_list[j]));
            }
        }
    });

But I am facing this error:

ReferenceError: clients_list is not defined

Help me please!

As always, encode as JSON .

{% load jsonify %}

var clients_list = {{ clients_list|jsonify }};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM