如何中断表单发布,直到按下确认按钮?

问题描述:

有这样的形式:如何中断表单发布,直到按下确认按钮?

<form action="" method="post"> 
    <input type="submit" value="Delete Me"> 
</form> 

我想将其更改为,按下提交按钮时,打开警告模式,如果按“确认”的模式,表单流程。

尝试一些代码,但我不知道还有什么办法来“继续”的形式处理后中断了,非常感谢。

$(function() { 
     $('.delete_form').on("submit",function(){ 
      $('#deleteModal').modal('toggle'); 
      return false; //pause the submit 
     }); 

     $('.confirm_del').on("click",function(){ 
      return true; //process the form submit 
     }); 
    }); 

使用下面的代码。 按钮被改成提交按钮正常按钮..

<form action="" method="post" id="f1"> 
    <input type="button" id="b1" value="Delete Me"> 
</form> 

<script> 
    $('#b1').click(function(){ 
     $('#deleteModal').modal('toggle'); 
    }); 

    $('.confirm_del').on("click",function(){ 
     $("#f1").submit(); //process the form submit 
    }); 
</script> 
+0

这是我会推荐的方式。确认时允许提交比未确认时取消提交更好。 – BobRodes

变化

type="submit" to type="button" 

,然后使用其ID或类添加事件侦听器然后打开了警告,并提交其响应值的形式。

+0

见吾DK的例子。 – BobRodes

​​3210

,直到用户接受警告只是不启动提交流程...

+0

'javascript:'URL在'onclick'属性中不起作用。 – rvighne

+0

糟糕。更正它 –

你的脚本应该是这样的:

$(function() { 
    $('.delete_form').on("submit",function(){ 
     return confirm('Are You Sure'); 
    }); 
}); 
+0

这将使它成为模式弹出(如警报),这不是OP想要的。他们正在使用他们自己的HTML模式。 – rvighne

您也可以触发点击确认按钮,当窗体的提交事件。

 $('.confirm_del').on("click",function(){ 
     $('.delete_form').trigger("submit") 
    }); 

试试这个,

<form action="" method="post" onsubmit="return isDeleteConfirm()"> 
     <input type="submit" value="Delete Me"> 
</form> 

function isDeleteConfirm(){ 
     $('.delete_form').on("submit",function(){ 
      $('#deleteModal').modal('toggle'); 
      return false; //pause the submit 
     }); 

     $('.confirm_del').on("click",function(){ 
      return true; //process the form submit 
     }); 
}