使用javascript进行表单操作 [英] form action with javascript
问题描述
我有一个必须在提交时执行javascript函数的表单,然后该函数将数据发布到我的php发送邮件文件并发送邮件。但它只适用于火狐。表单操作似乎没有在IE中做任何事情,我的问题是:这是从表单操作从外部文件调用函数的正确方法:
I have a form that must execute a javascript function on submit, the function then posts data to my php send mail file and the mail is sent. But it only works in fire fox. The form action does not seem to be doing anything in IE, my question is: Is this the correct way to call a function from an external file from the form action:
action="javascript:simpleCart.checkout()"
simpleCart是.js文件和checkout()是函数。
simpleCart is the .js file and checkout() is the function.
提示赞赏,努力理解为什么它可以在Firefox中工作,但不是IE,chrome或safari。
Tips appreciated, struggling to understand why it would work in firefox but not IE, chrome or safari.
<form name=form onsubmit="return validateFormOnSubmit(this)" enctype="multipart/form-data" action="javascript:simpleCart.checkout()" method="post">
推荐答案
设置为JavaScript函数的表单操作并不广泛支持,我很惊讶它在FireFox中运行。
A form action set to a JavaScript function is not widely supported, I'm surprised it works in FireFox.
最好是将表单 action
设置为PHP脚本;如果您在提交之前需要做任何事情,可以添加到 onsubmit
The best is to just set form action
to your PHP script; if you need to do anything before submission you can just add to onsubmit
编辑原来你不需要任何额外的功能,这里只是一个小小的改变:
Edit turned out you didn't need any extra function, just a small change here:
function validateFormOnSubmit(theForm) {
var reason = "";
reason += validateName(theForm.name);
reason += validatePhone(theForm.phone);
reason += validateEmail(theForm.emaile);
if (reason != "") {
alert("Some fields need correction:\n" + reason);
} else {
simpleCart.checkout();
}
return false;
}
然后在您的表格中:
<form action="#" onsubmit="return validateFormOnSubmit(this);">
这篇关于使用javascript进行表单操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!