对于一个带有警告的简单表单,询问字段是否填写正确,我需要一个执行此操作的函数:
使用两个选项单击按钮时显示警告框:
我认为JavaScript确认可行,但我似乎无法弄清楚如何。
我现在的代码是:
function show_alert() {
alert("xxxxxx");
}
<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>
答案 0 :(得分:339)
一个简单的内联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);">
答案 1 :(得分:24)
function show_alert() {
if(confirm("Do you really want to do this?"))
document.forms[0].submit();
else
return false;
}
答案 2 :(得分:19)
您可以使用JS确认功能。
<form onSubmit="if(!confirm('Is the form filled out correctly?')){return false;}">
<input type="submit" />
</form>
答案 3 :(得分:2)
简单:
f()
答案 4 :(得分:1)
好的,只需将您的代码更改为以下内容:
<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?');
}
&#13;
<form onsubmit="return submitForm(this);">
<input type="text" border="0" name="submit" />
<button value="submit">submit</button>
</form>
&#13;
答案 5 :(得分:0)
如果要对表单提交应用某些条件,则可以使用此方法
<form onsubmit="return checkEmpData();" method="post" action="process.html">
<input type="text" border="0" name="submit" />
<button value="submit">submit</button>
</form>
请始终记住,方法和操作属性是在提交后属性
之后写入的javascript代码
function checkEmpData()
{
var a = 0;
if(a != 0)
{
return confirm("Do you want to generate attendance?");
}
else
{
alert('Please Select Employee First');
return false;
}
}