简体   繁体   English

有没有办法用一个按钮和唯一的隐藏值提交多个表单?

[英]Is there a way to submit multiple forms with a single button and unique hidden values?

Note: I have seen some posts that are quite similar to mine but I don't think those solutions help in my use case.注意:我看过一些与我的非常相似的帖子,但我认为这些解决方案对我的用例没有帮助。 My website (made using Django) has a page for collecting dues payments.我的网站(使用 Django 制作)有一个页面用于收取会费。

There is one page per family, in which there will be multiple dues to be paid by each member.每个家庭有一页,其中每个成员将支付多笔会费。 Currently, I have one submit button to each due as you can see in the code below.目前,我有一个提交按钮给每个到期,你可以在下面的代码中看到。

{% for due in dues %}
<tr>
    <td>{{due.0}}</td>
    <td>{{due.1}}</td>
    <td>{{ due.3}}</td>
    <td>{{ due.4 }}</td>
    <td>{{ due.5 }}</td>
    <td><form action="/dues/?family-dues={{ familyprofile }}" method="POST" id= "dues-payment">
        {% csrf_token %}
                    <input type="text" placeholder="Enter Amount" name="amount">
                    <input type="hidden" value="{{due.2}}" name="due-id">
                    <input type="hidden" value="{{due.0}}" name="member-id">
                    <input type="hidden" value="{{duefamily.0}}" name="family-id">
        <button id="submit-payment">Pay</button></form></td>
</tr>
    {% endfor %}

The issue with this is that, only one payment can be done at a time.问题在于,一次只能进行一次付款。 Is there a way where I can instead submit all the forms in one go.有没有一种方法可以让我一次性提交所有表格。 So that the cashier can just enter the appropriate payments in each textbox and when its submitted all the textbox values(paid amount) along with the due id be returned in one go?这样收银员就可以在每个文本框中输入适当的付款,并在提交所有文本框值(已付款金额)以及到期 ID 时一次性返回?

Current Scenario当前情景

Due ID 1      textbox(Value is 40)       Pay Button
Due ID 2      textbox(Value is 80)       Pay Button
Due ID 3      textbox(Value is 100)      Pay Button

If a person come to pay a total of $220 (as shown above) across three payment due categories, the pay button needs to be pressed thrice.如果一个人在三个到期付款类别中总共支付了 220 美元(如上所示),则需要按三次支付按钮。 Is there a way the above can be changed to the below scenario.有没有办法将上述情况更改为以下情况。

Desired Scenario期望的场景

Due ID 1      textbox(Value is 40)
Due ID 2      textbox(Value is 80) 
Due ID 3      textbox(Value is 100)
Pay Button

I am open to all ideas that can help me solve my issue.我对所有可以帮助我解决问题的想法持开放态度。 Thanks in advance:)提前致谢:)

The simplest approach would be to make the whole table one form.最简单的方法是使整个表格成为一种形式。

<form action="/dues/?family-dues={{ familyprofile }}" method="POST" id= "dues-payment">
    {% csrf_token %}
    <table>
        {% for due in dues %}
          <tr>
            <td>{{ due.0 }}</td>
            <td>{{ due.1 }}</td>
            <td>{{ due.3 }}</td>
            <td>{{ due.4 }}</td>
            <td>{{ due.5 }}</td>
            <td>
              <input type="text" placeholder="Enter Amount" name="due-{{ due.2 }}-amount">
            </td>
          </tr>
        {% endfor %}
    </table>
    <input type="hidden" value="{{ ?? }}" name="member-id">
    <input type="hidden" value="{{ duefamily.0 }}" name="family-id">
    <button id="submit-payment">Pay</button>
</form>

Note that I added the due IDs to the names of the fields that are shown on each line.请注意,我将到期 ID 添加到每行显示的字段名称中。 Now you will have to do a bit of string parsing on the Django side to match these together.现在,您必须在 Django 端进行一些字符串解析才能将它们匹配在一起。

You might also want to look into Django Formsets , which allow you to define the whole form in Python and just call form.as_table() in the template.您可能还想查看Django Formsets ,它允许您在 Python 中定义整个表单并在模板中调用form.as_table() It also has support for nested forms like this one.它还支持像这样的嵌套表单。

Edit编辑

I think you should also be able to do the amount inputs like this and then get lists in Django, but unfortunately I can't test if it works.我认为您也应该能够像这样输入数量,然后在 Django 中获取列表,但不幸的是,我无法测试它是否有效。 You would get one called due-id and one called due-amount , and then rely on the values for each index matching.你会得到一个叫做due-id和一个叫做due-amount ,然后依赖于每个索引匹配的值。

        <td>
          <input type="text" placeholder="Enter Amount" name="due-amount[]">
          <input type="hidden" value={{ due.2 }} name="due-id[]">
        </td>

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

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