【问题标题】:How to submit a form using sweet alert confirm?如何使用甜蜜警报确认提交表单?
【发布时间】:2017-07-16 18:58:42
【问题描述】:

我在使用sweet alert 确认后尝试提交表单。但是,不知何故,我的表单绕过了甜蜜警报确认(未经确认发送)。

我想做的是。

  • 按提交
  • 显示甜蜜警报
  • 如果点击是 = 发送表单
  • 如果单击取消 = 关闭甜蜜警报

这是我的脚本

$("#btn_submit").on('click',function(e){ //also can use on submit
    e.preventDefault(); //prevent submit
    var jns_srt = $("#i_dok").val();

    swal({
        title: "Are you sure?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes!",
        cancelButtonText: "Cancel",
        closeOnConfirm: true
    }, function () { 
        //I don't know what to write here. 
        });
});

 <form name="frm_input" id="frm_input_srt" method="post" action="<?php echo site_url('Admin/rekam_srt_msk'); ?>">
<table>
 <tr><td colspan="3" style="text-align:left; padding:0; vertical-align:middle"><input type="text" name="test" /></td></tr>
            <tr>
                <td colspan="3" style="text-align:center; padding:0; vertical-align:middle">
                    <input type="reset" id="btn_reset" name="btn_reset" class="btn btn-danger" value="Reset">
                    &nbsp;||&nbsp;
                    <input type="submit" id="btn_submit" name="btn_submit" class="btn btn-success" value="Save">
                </td>
            </tr>
            </table>
 </form>

【问题讨论】:

    标签: javascript php jquery forms sweetalert


    【解决方案1】:

    文档展示了如何在取消和确认时运行不同的代码。直接来自他们的site

    swal({
      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete it!",
      cancelButtonText: "No, cancel plx!",
      closeOnConfirm: false,
      closeOnCancel: false
    },
    function(isConfirm){
      if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
      } else {
        swal("Cancelled", "Your imaginary file is safe :)", "error");
      }
    });
    

    对于您的代码:

    $("#frm_input_srt").submit( function () {
        var jns_srt = $("#i_dok").val();
    
        swal({
            title: "Are you sure?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes!",
            cancelButtonText: "Cancel",
            closeOnConfirm: true
        }, function(isConfirm){
          if (isConfirm) {
            return true;
          } else {
            return false;
          }
        });
    });
    

    【讨论】:

    • 确认窗口仍然没有停止。当我点击按钮时,我的表单在没有确认的情况下发送
    • 我已经编辑过了。形式简化(我的实际形式很复杂)
    • 很遗憾没有。
    • 是否有任何 js 代码可能会冲突/覆盖此代码?或者任何 console.log 错误?
    【解决方案2】:

    您可以通过 Id 获取表单元素然后提交。

    document.getElementById("frm_input_srt").submit();
    

    或者jquery方式

    $("#frm_input_srt").submit();
    

    【讨论】:

      【解决方案3】:
       }, function () { 
       //to submit:
       document.getElementById("frm_input_srt").submit();
       });
      

      【讨论】:

      • 它仍在发送,我没有点击是
      【解决方案4】:

      使用这个:

      $("#btn_submit").on('click',function(e){ //also can use on submit
          e.preventDefault(); //prevent submit
          var jns_srt = $("#i_dok").val();
      
          swal({
              title: "Are you sure?",
              type: "warning",
              showCancelButton: true,
              confirmButtonColor: "#DD6B55",
              confirmButtonText: "Yes!",
              cancelButtonText: "Cancel",
              closeOnConfirm: true
          }, function (getAction) { 
      if (getAction.value === 'true') {
              // Insert code that will be run when user clicks Ok.
              });
      });
      

      【讨论】:

        【解决方案5】:

        这是你的答案

        ("#btn_submit").on('click',function(e){ //also can use on submit
        e.preventDefault(); //prevent submit
        swal({
            title: "Are you sure?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes!",
            cancelButtonText: "Cancel",
            closeOnConfirm: true
        }
        }).then(function(value) {
            if (value) {
              $('#frm_input_srt').submit();
            }
        });
        

        【讨论】:

          【解决方案6】:

          更新日期:2021

          $("#frm_input_srt")
          .submit(async () => {
              const jns_srt = $("#i_dok").val();
          
              const result = await swal({
                  title: "Are you sure?",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonColor: "#DD6B55",
                  confirmButtonText: "Yes!",
                  cancelButtonText: "Cancel",
                  closeOnConfirm: true
              });
              
              if(!result.isConfirmed)
                  event.preventDefault();
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-09-17
            • 1970-01-01
            • 1970-01-01
            • 2017-01-04
            • 2023-04-03
            • 2021-10-22
            • 1970-01-01
            相关资源
            最近更新 更多