【问题标题】:How to send dynamic modal form fields to django如何将动态模态表单字段发送到 django
【发布时间】:2019-09-15 09:19:27
【问题描述】:

我正在创建动态模态表单字段,我想将生成的字段值发送到 django,但只有一个字段发送到我的 django 服务器。 如果这是由于输入字段具有相同的名称而引起的,我会怀疑。 请问有什么建议吗?

   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
var max_fields = 10;
var wrapper = $(".container1");
var add_button = $(".add_form_field");

var x = 1;
$(add_button).click(function(e) {
    e.preventDefault();
    if (x < max_fields) {
        x++;
        $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="delete">Delete</a></div>'); //add input box
    } else {
        alert('You Reached the limits')
    }
});

$(wrapper).on("click", ".delete", function(e) {
    e.preventDefault();
    $(this).parent('div').remove();
    x--;
})

});

                      <div class="modal fade" id="multipleset{{ecriture.id_ecriture}}" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
                             <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                   <div class="modal-header">
                                      <h6 class="modal-title" id="exampleModalLabel">Ajouter une &eacute;criture</h6>
                                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                      <span aria-hidden="true">&times;</span>
                                      </button>
                                   </div>

                                  <form action="{% url 'multipleset' %}" method="post" enctype="multipart/form-data">{% csrf_token %}
                                    <div class="modal-body">
                                      <div class="container1">
                                          <button class="add_form_field">Add New Field &nbsp;
                                            <span style="font-size:16px; font-weight:bold;">+ </span>
                                          </button>
                                          <div><input type="text" name="mytext[]"></div>
                                      </div>
                                    </div>
                                    <div class="modal-footer">
                                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
                                      <button type="submit" class="btn btn-primary">Ajouter</button>
                                    </div>
                                  </form>
                                </div>
                             </div>
                      </div>

【问题讨论】:

    标签: javascript django django-forms bootstrap-4


    【解决方案1】:

    您应该像下面给出的那样动态更改输入名称的名称。我认为这应该可以解决问题。 (未测试)请试一试。

    $(wrapper).append('<div><input type="text" name="mytext'+x+'"/><a href="#" class="delete">Delete</a></div>'); //add input box
    

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 2015-02-14
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 2011-09-28
      • 1970-01-01
      相关资源
      最近更新 更多