在提交表单不起作用之前返回确认对话框

时间:2016-01-02 20:33:58

标签: javascript php forms form-submit confirm

我在提交表单之前试图让网站返回确认对话框,但由于某种原因它无法正常工作。它返回了确认对话框,但点击“是”后,它不会提交表单。我做错了什么?

<?php

echo "
<form action = 'delete.php' method = 'POST'>
<label id = 'delete' onclick = \"return confirm('Are you sure?');this.form.submit()\"> delete </label>
</form>
";

?>

注意:

而不是使用input:submit,我宁愿使用标签onclick()和确认对话框。那可能吗?感谢。

1 个答案:

答案 0 :(得分:1)

代码中的onclick处理程序只返回confirm对话框结果,并忽略代码的其余部分(this.form.submit()); 我建议在这种情况下使用外部函数:

<?php

echo "
<form action = 'delete.php' method = 'POST'>
<label id = 'delete' onclick = \"confirmSubmit(this);\"> delete </label>
</form>
";

?>

js code:

function confirmSubmit(e){
    var need_submit = confirm('Are you sure?');
    if (need_submit) e.form.submit();
}