用Django上下文数据填充bootbox.js提示

时间:2019-03-26 16:00:02

标签: python jquery django bootbox

我想做的是在提示符下获取text值,以与从视图接收的数据相匹配。 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>

但这只是多次显示相同的提示,而每次都使用不同的名称。

1 个答案:

答案 0 :(得分:2)

您的循环正在创建相同的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>