【问题标题】:Form with multiple submit buttons directing to different URL带有多个提交按钮的表单指向不同的 URL
【发布时间】:2021-02-14 02:49:47
【问题描述】:

我想要一个带有两个提交按钮 Check InCancel 的表单,如下所示:

<div id="table-checkin-form">
  <form class="checkin-form-container" method="post">
    {{ checkin_form.as_p }}
    {% csrf_token %}
    <a href="site_1">
      <button type="submit" class="button-table-checkin">Check In</button>
    </a>
      <button type="submit" class="button-table-checkin-cancel">Cancel</button>
  </form>
</div>

当点击Check In 按钮时,我想定向到网址site_1。如果单击了Cancel,则什么也不做(即关闭表单并停留在同一页面上)。但以上不起作用。在任何一种情况下,表单都会关闭而不转到site_1

但是如果我把网址site_1 放进去

<form class="checkin-form-container" method="post" action="site_1">

然后单击这两个按钮会将我带到site_1

我应该如何实现这种情况?谢谢。

【问题讨论】:

  • “取消按钮”最好使用 type="button"。这样它就不会将表单提交到您的操作 URL
  • 谢谢大家。我刚刚找到了这个线程here,它为完全相同的情况提供了一个很好的工作解决方案。

标签: html forms django-templates


【解决方案1】:

您可以替换 &lt;a&gt; 来代替您的按钮之一:

<button type="submit" class="button-table-checkin">Check In</button>
<a class="button-table-checkin-cancel" href="site_2">Cancel</a>

或添加onclick到&lt;a&gt;

【讨论】:

    【解决方案2】:

    我不确定,但你需要在按钮内写下锚标记然后它才会起作用。

    <div id="table-checkin-form">
        <form class="checkin-form-container" method="post">
            {{ checkin_form.as_p }}
            {% csrf_token %}
            <button type="submit" class="button-table-checkin"> <a href="site_1">Check In</a></button>
            <button type="submit" class="button-table-checkin-cancel">Cancel</button>
        </form>
    </div>
    

    【讨论】:

      【解决方案3】:

      相信你可以直接将“site_1”的链接添加到按钮的创建中。

      <button type="submit" class="button-table-checkin" onclick="location.href='Site_1'">Check In</button>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-12
        • 2012-09-20
        • 1970-01-01
        • 1970-01-01
        • 2011-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多