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

Populating bootbox.js prompt with django context data

我想做的是获取提示中的 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>

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

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