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

问题描述 投票:0回答:1

我要做的是在提示符中获取textvalue以匹配从视图接收的数据。 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>

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

python jquery django bootbox
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.