如何使用甜蜜警报确认提交表单?

时间:2017-02-26 02:43:24

标签: javascript php jquery forms sweetalert

我正在尝试使用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>

5 个答案:

答案 0 :(得分: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;
      }
    });
});

答案 1 :(得分:0)

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

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

或jquery方式

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

答案 2 :(得分:0)

 }, function () { 
 //to submit:
 document.getElementById("frm_input_srt").submit();
 });

答案 3 :(得分:0)

使用此:

$("#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.
        });
});

答案 4 :(得分:0)

这是你的答案

("#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();
    }
});