2011-06-29 65 views
120

对于一个简单的形式,询问领域中填写正确的警告,我需要做这样的功能:JavaScript的表单提交 - 确认或取消提交对话框

  • 显示一个提示框时,按钮点击有两个选项:

    • 如果“OK”被点击,表单提交
    • 如果取消被点击,警告框关闭和形式可以进行调整和重新提交

我认为一个JavaScript的确认工作,但我似乎无法弄清楚如何。

我现在的代码是:

<script> 
function show_alert() { 
    alert("xxxxxx"); 
} 
</script> 
<form> 
    <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" 
     alt="PayPal - The safer, easier way to pay online!" value="Submit"> 
</form> 

回答

265

简单联JavaScript确认就足够了:

<form onsubmit="return confirm('Do you really want to submit the form?');"> 

无需一个外部功能除非你正在做验证,你可以做一些事情像这样:

<script> 
function validate(form) { 

    // validation code here ... 


    if(!valid) { 
     alert('Please correct the errors in the form!'); 
     return false; 
    } 
    else { 
     return confirm('Do you really want to submit the form?'); 
    } 
} 
</script> 
<form onsubmit="return validate(this);"> 
+0

当onClick =“this.form.submit(); this.disabled = true; this.value ='正在发送...';”'''提交按钮时,弹出窗口不会显示在Chrome中它适用于Firefox – shintaroid

+1

@shintaroid如果还有其他问题,请点击[问问题](// stackoverflow.com/questions/ask)按钮。 –

23
function show_alert() { 
    if(confirm("Do you really want to do this?")) 
    document.forms[0].submit(); 
    else 
    return false; 
} 
+1

此代码将分首先在文档中形成表单。 – user254153

17

您可以使用JS确认功能。

<form onSubmit="if(!confirm('Is the form filled out correctly?')){return false;}"> 
    <input type="submit" /> 
</form> 

http://jsfiddle.net/jasongennaro/DBHEz/

+21

'onsubmit =“return confirm('表单填写是否正确?');”'会简单得多,结果也是一样的。 – Sk8erPeter

2
<form onsubmit="return confirm('Do you really want to submit the form?');"> 
1

OK,只是改变你的代码是这样的:

<script> 
function submit() { 
    return confirm('Do you really want to submit the form?'); 
} 
</script> 

<form onsubmit="return submit(this);"> 
    <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" 
     alt="PayPal - The safer, easier way to pay online!" value="Submit"> 
</form> 

而且这是在运行的代码,只是我更容易地看到它是如何工作的,只要运行下面的代码看到的结果:

function submitForm() { 
 
    return confirm('Do you really want to submit the form?'); 
 
}
<form onsubmit="return submitForm(this);"> 
 
    <input type="text" border="0" name="submit" /> 
 
    <button value="submit">submit</button> 
 
</form>