简体   繁体   English

在不丢失“提交”按钮值的情况下恢复中断的表单提交

[英]Resuming an interrupted form submit without losing the "submit" button value

I have a simple function that intercepts a form submit event, displays a SweetAlert2 confirmation dialogue, and if the user confirms the form is submitted.我有一个简单的函数,它拦截表单提交事件,显示 SweetAlert2 确认对话框,如果用户确认表单已提交。

The function is something like this:函数是这样的:

$(document).ready(function() {
    $('form.require-confirmation').submit(event => window.swalConfirm(event));
});
window.swalConfirm = function(e)
{
    e.preventDefault();

    Swal.fire().then((result) => {
        if (result.value) {
            e.target.submit();
        }
    })
};

This works as expected… in most cases.这按预期工作……在大多数情况下。 When the form has multiple submit buttons – for example:当表单有多个提交按钮时——例如:

<button type="submit" name="action" value="add">Add</button>
<button type="submit" name="action" value="remove">Remove</button>

… the value of "action" is removed from the submitted form. ... "action" 的值从提交的表单中删除。 Is there any way around this, like resuming the initial submit or something like that?有没有办法解决这个问题,比如恢复初始提交或类似的东西? Or is there no option but making the buttons change the value of a hidden input before submitting the form, for example?或者除了让按钮在提交表单之前更改隐藏输入的值之外别无选择,例如?

You gave the solution yourself你自己给出了解决方案

<input type="hidden" name="action" value="" />
<button type="button" class="action" value="add">Add</button>
<button type="button" class="action" value="remove">Remove</button>

$(function() {
  $('.action').on("click",function(e) {
    const parent = this.form;
    $("[name=action]").val(this.value);
    Swal.fire().then((result) => {
      if (result.value) {
        parent.submit();
      }
    })
  });
});

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

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