简体   繁体   中英

django template: dynamic variable name in javascript

In my javascript i want something like this

SomeFunction(attribute){
    var prev_rating = {{ u_list.attribute }};
}

The function can have three different inputs... beauty, wealth, age . So if I pass attribute = beauty to the function, var prev_rating should be set to

var prev_rating = {{ u_list.beauty }};

How can I achieve this ?

You have to write the value selection in Javascript:

var ratings = {beauty: {{u_list.beauty}}, wealth: {{u_list.wealth}}, age: {{u_list.age}} }
function SomeFunction(attribute) {
    var prev_rating = ratings[attribute]
}

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