如何使用JavaScript函数在表单元素提交时提供确认对话框

5

HTML 代码:

<form action='save.php' method="post" onsubmit="return validate(this)">
  some inputs here...
</form>

这里的验证功能正常,但确认代码不起作用。 Javascript 代码:

function validate(theForm)
{
    validations here for input fields....
    /*confirm dialog*/
    var sure = confirm("Are you sure to proceed ?");
    if(sure == false)
    {
        return false;
    }
    else
    {
        return sure;
    }
}

确认对话框会显示,但是在单击“确定”按钮时,它不会提交表单,是否有办法使用JavaScript函数在确认对话框上提交表单。提前致谢。


4
顺便提一下:为什么不直接使用return confirm('Are you sure?')而不是整个if-else检查呢? - tewathia
一个简单的内联JavaScript确认会起到作用。例如, <form onsubmit="return confirm('您确定要继续吗?');"> - Dipesh Parmar
什么没有起作用?[http://jsfiddle.net/b8LrB/1/] - Kirill
谢谢tewathia,它运行得很好。 :) - NiksD
1
@DipeshParmar 不一定,因为该函数验证所有输入。但是,正如tewathia所说,if-else可以用那行代码替换。 - Trojan
显示剩余4条评论
1个回答

2

根据您的要求,回答我的评论:

将表单验证函数编写为:

function validate(theForm){
    //validation stuff here
    return confirm('Are you sure you want to proceed?');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接