这就是我所拥有的:
<form name="myts" method="post" action="ts.php" enctype="multipart/form-data" onsubmit="return upuInit(this)">
.
.
...bunch of inputs/buttons etc...
.
.
<input type="hidden" id="isFormValidated" name="isFormValidated" value="">
</form>
<input name="image" type="image" src="img/submit.png" border="0" onClick="validateTS(document.forms['myts']);alert('yo iam back. val: '+document.forms['myts'].value);if(document.forms['myts'].isFormValidated.value == 'true') {alert('about to submit');document.forms['myts'].submit();}">
这是调用 PHP 验证脚本 (AJAX) 的“validateTS”javascript。
function validateTS(oForm) {
var x = GetXmlHttpObject();
var errMsg = "";
var url = "validateTS.php?usr=TEST";
var retVal = false;
// PC Work Hours
var pcWH = getElement("txtpcwk");
var pcTH = getElement("txtpctrv");
var pcTot = parseInt(pcWH.value) + parseInt(pcTH.value);
url += "&pcHrs0="+pcTot;
// Build the URL above and include the "updated" values.
var selectBox = getElement("addlst");
var myOptions = [];
for (var loop=0; loop<selectBox.options.length; loop++) {
myOptions[loop] = { optText:selectBox.options[loop].text, optValue:selectBox.options[loop].value };
}
for (var loop=0; loop<myOptions.length; loop++) {
var assWH = getElement("txtasswk_"+myOptions[loop].optValue);
var assTH = getElement("txtasstrv_"+myOptions[loop].optValue);
var assWHTotal = parseInt(assWH.value) + parseInt(assTH.value);
url += "&emp"+loop+"="+assWHTotal;
}
var tsApprover = getElement("txtappr").value;
var tsClientRep = getElement("txtins").value;
url += "&tsApprover="+tsApprover+"&tsClientRep="+tsClientRep;
x.open("GET",url,true);
x.onreadystatechange=function() {
if(x.readyState == 4 && x.status == 200) {
var r = x.responseText;
var myUploadAlerts = "";
var isUploadFilesValid = false;
myUploadAlerts = validateUploadForm(oForm);
r = r.trim();
myUploadAlerts = myUploadAlerts.trim();
if (r != "" || myUploadAlerts != "") {
showMyAlerts("Please correct the following Error(s) before proceeding: <br><br>" + formatAlerts(r) + formatAlerts(myUploadAlerts), 500, block_ui_enabled);
oForm.isFormValidated.value = "false";
}
else {
oForm.isFormValidated.value = "true";
}
}
};
x.send(null);
}
现在我有 2 个主要问题:1)当我点击输入图像(在表单中)时,它会进入 ValidateTS 函数并验证表单并且没有任何问题。并在有任何错误时显示警报。但它没有在表单中设置“isFormValidated”值......不知道为什么......也许我可以返回其他东西而不是设置表单值?
2)这给了我更多的 greif 比其他一切。在整个表单中,我有一个“上传”部分,用户可以上传任何内容。但是我只有一个按钮(要提交的表单中的图像按钮)。因此,当我单击图像时,它首先验证表单,如问题 1 中所述,然后最终“提交”表单。现在,当我调用 javascript 提交函数时,我假设它会在表单上执行“ONSUBMIT”函数(这不会执行...并且表单最终会提交到 ts.php)。
我想要它,所以当我单击输入图像(提交图像按钮)时,表单得到验证,如果 isFormValidated 为 TRUE,我运行“document.myts.submit()”然后 onsubmit="return upuInit(this)"被执行。请帮助我在这方面花了太多时间,现在我被困住了。