简体   繁体   English

如何在javascript中更改twig内部的变量

[英]How to change variable inside twig in javascript

I want to change the variable inside Twig by javascript, but there is an error: Variable "max" does not exist.我想把Twig里面的变量改成javascript,但是报错:变量“max”不存在。

my twig:我的 twig:

{% block body %}
    <label for="maxN">Niveau MAX</label>
    <input type="number" id="maxN" class="form-control">

    {{ form_label(form.ncUoCommandeUo) }}
    {{ form_widget(form.ncUoCommandeUo, {'attr':{'min':1, 'max': max}}) }}
{% endblock %}

{% block javascripts %}
    <script>

      my.ajax......

      if (ncMaxUo.val() === 1) {
        max = 1;
      } elseif(ncMaxUo.val() === 2) {
        max = 2;
      } elseif(ncMaxUo.val() === 3) {
        max = 3;
      } else {
        max = 4;
      }
      ....
    </script>
{% endblock %}

Just update my solution.只需更新我的解决方案。

{% block body %}
<label for="maxN">Niveau MAX</label>
<input type="number" id="maxN" class="form-control">

{{ form_label(form.commande) }}
{{ form_widget(form.commande, {'attr':{'min':1}}) }}
{% endblock %}

{% block javascripts %}
<script>

 my.ajax......

var maxN = $("#maxN");
maxN = ???;(get value from ajax)

 $("#commande").attr("max", maxN.val());
....
</script>
{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM