我有一个在 Chrome 中开发的 PHP 表单验证函数,现在无法在 Firefox 或 Opera 中运行。
该函数检查表单的某个部分是否为空白并显示错误消息。如果没有错误,则表单通过 document.events.submit();
代码:
function submit_events()
{
//Check to see if a number is entered if the corosponding textbox is checked
if (document.events.dj_card.checked == true && dj_amount.value==""){
//Error Control Method
//alert ('You didn\'t enetr an Amount for DJ\'s Card!');
var txt=document.getElementById("error")
txt.innerHTML="<p><font color=\"#FF0000\"> You didn\'t enetr an Amount for DJ\'s Card!</font></p>";
window.document.getElementById("dj_card_label").style.color = '#FF0000';
//Reset
window.document.getElementById("company_amount_label").style.color = '#000000';
window.document.getElementById("own_amount_label").style.color = '#000000';
}else{
document.events.submit();
}
document.events.submit();
确实适用于我的所有浏览器,但 check 语句却不能。
如果未勾选该框,则提交表单。如果勾选该框,则 dj_amount.value 中是否有数据并不重要。该表单将不会提交,并且不会显示任何错误消息。
谢谢大家。
最佳答案
以下是我注意到的一些事情。不确定它是否能解决问题,但您需要修复其中的一些问题;其中一些只是观察。
- dj_amount 未声明或引用;我猜你的意思是
documents.events.dj_amount
- 您应该在 javascript 中每个语句的末尾添加
;
,包括var txt = document.getElementById("error") 的末尾
- 您不需要转义
txt.innerHTML
行中的字符串;您只需要像引号一样转义,例如"\""
或'\''
,而不是"'"
或' “'
- 您不需要引用window.document;文档几乎适用于所有情况
编辑 - 正如 Guffa 指出的那样,FONT 是 HTML 中一个古老且已弃用的元素。这不是问题的原因,但现代标记方法意味着您不需要它。考虑省略样式并将其应用到段落标记。
请参阅下面的编辑。
function submit_events() {
//Check to see if a number is entered if the corosponding textbox is checked
if (document.events.dj_card.checked == true && document.events.dj_amount.value == "") {
//Error Control Method
//alert ('You didn't enetr an Amount for DJ\'s Card!');
var txt = document.getElementById("error");
txt.innerHTML = "<p style=\"color: #FF0000;\"> You didn't enter an Amount for DJ's Card!</p>";
document.getElementById("dj_card_label").style.color = '#FF0000';
//Reset
document.getElementById("company_amount_label").style.color = '#000000';
document.getElementById("own_amount_label").style.color = '#000000';
} else {
document.events.submit();
}
}
考虑 Firebug,以便您可以查看并记录到控制台 JavaScript 错误和消息:
关于JavaScript 无法在 Firefox 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4914027/