我要做的是在提示符中获取text
value以匹配从视图接收的数据。 bootbox.js看起来像这样:
<script>
$('#playGameBtn').click(function(){
bootbox.prompt({
title: "Please select players for this match",
value: ['1', '3'],
inputType: 'checkbox',
inputOptions: [{
text: '{{standing.player_name}}',
value: '1',
},
{
text: 'Choice Two',
value: '2',
},
{
text: 'Choice Three',
value: '3',
}],
callback: function (result) {
console.log(result);
}
});
}
)
</script>
我试过的是这个:
<script>
{%for standing in standings%}
$('#playGameBtn').click(function(){
bootbox.prompt({
title: "Please select players for this match",
value: ['1', '3'],
inputType: 'checkbox',
inputOptions: [{
text: '{{standing.player_name}}',
value: '1',
},
{
text: 'Choice Two',
value: '2',
},
{
text: 'Choice Three',
value: '3',
}],
callback: function (result) {
console.log(result);
}
});
}
)
{%endfor%}
</script>
但这只是每次都以不同的名称多次显示相同的提示。
你的循环正在创建相同的javascript来一次又一次初始化bootbox
。你只需循环选项。像这样
<script>
$('#playGameBtn').click(function () {
bootbox.prompt({
title: "Please select players for this match",
value: ['1', '3'],
inputType: 'checkbox',
inputOptions: [
{% for standing in standings %}
{
text: '{{standing.player_name}}',
value: '{{ forloop.counter }}'
},
{% endfor %}
],
callback: function (result) {
console.log(result)
}
})
}
)
</script>