使用Sweet Alert(aka Swal)提交表格

时间:2018-01-30 14:13:09

标签: javascript jquery forms submit sweetalert

我使用Swal替换所有JavaScript默认警报和内容。 我的一个页面上有一个表单,我也使用Swal要求用户确认其操作(删除文件)。

问题是我无法使表单正常工作。这是Swal代码:

const int a = 2;
int b = 2;

int main()
{
  function_A(&a);  // a is constant => results in undefined behaviour
                   // when function_B tries to modify the data

  function_A(&b);  // OK
}

这是我的提交按钮:

jQuery('.deleteFile').click(function(event){
    event.preventDefault();
    swal({
        title: "You sure about deleting this file?",
        icon: "warning",
        buttons: {
            cancel: "hmm let me think this through",
            confirm: "100% sure !",
        },
        dangerMode: true,
    })
    .then((willDelete) => {
        if (willDelete) {
            $('#form1').submit();
        } else {
            return false;
        }
    });
});

这里有几点要提及:

  • 在JavaScript / jQuery方面,我完全是个新手。所以如果你在这里发现任何搞笑的错误,请原谅我的新秀< 3

  • 我对此进行了大量搜索并尝试了我发现的所有建议,例如使用许多用户建议的isConfirm功能。但都没有效果。使用isConfirm函数实际上会破坏整个代码,甚至警报部分也不会弹出。

  • .then((willDelete)部分代码是Swal的默认代码,实际上工作正常。

  • 类deleteFile指向提交按钮。 ID form1指向表单本身。

  • 在代码的当前状态下,当用户按下确认按钮时,页面会刷新,看起来已完成作业。但它实际上不起作用。我已经编写了以下代码来测试它:

    <button type="submit" name="delete_file" class="btn btn-danger deleteFile">Delete</button>
    

但它并没有回应任何东西(QQ)

毋庸置疑,delete_file是我提交按钮的名称属性。

1 个答案:

答案 0 :(得分:1)

  

毋庸置疑,delete_file是我提交按钮的名称属性。

那就是问题所在。只有单击该按钮,才会提交提交按钮的名称。如果您使用JavaScript提交表单,则不会提交提交按钮名称和值。

为了防止出现这种情况,您可以将其添加到隐藏的输入中,而不是在提交按钮中添加名称/值:

<input type="hidden" name="delete_file" value="Delete">

或者您可以检查是否设置了其他字段,例如您要在案例中删除的文件的ID。

相关问题