如果在其他地方已经回答了这个问题我会道歉(我事先检查过,但无法找到答案)。
问题似乎是当我点击提交时验证脚本没有运行。
以下是代码:
window.onload = function() {
document.forms[0].onsubmit = validForm;
}
function validForm() {
var allGood = true;
var allTags = document.forms[0].getElementsByTagName("*");
for (var i=0; i<allTags.length; i++) {
if(!validTag(allTags[i])) {
allGood = false;
}
}
return allGood;
function validTag(thisTag) {
var outClass = "";
var allClasses = thisTag.className.split(" ");
for (var j=0; j<allClasses.length; j++) {
outClass += validBasedOnClass(allClasses[j]) + " ";
}
thisTag.className = outClass;
if (outClass.indexOf("invalid") > -1) {
thisTag.focus();
if (thisTag.nodeName == "INPUT") {
thisTag.select();
}
return false;
}
return true;
function validBasedOnClass(thisClass) {
var classBack = "";
switch(thisClass) {
case "":
case "invalid":
break;
case "reqd":
if(allGood && thisTag.value == "") {
classBack = "invalid";
}
classBack += thisClass;
break;
default:
classBack += thisClass;
}
return classBack;
}
}
}
&#13;
input.invalid {
background-color: yellow;
border: 1px solid red!important;
}
&#13;
<form action="#" id=quote-generator>
<input type=number step=1 placeholder=Distance id=distance class=reqd autofocus>
<input type=text value="Running Cost" id=fuelPrice onFocus="this.blur()" readonly><span id=aside title="This value is calculated by the AA & is updated every 3 months">*</span>
</form>
<input type=submit value="Submit" form=quote-generator onclick="formQuote()">
&#13;
这是fiddle。
答案 0 :(得分:0)
您正在尝试调用未定义的函数formQuote
,将其更改为validForm
,该代码用于验证表单
看到这个工作小提琴:https://jsfiddle.net/jrx5uhju/1/
<强>码强>
<input type=submit value="Submit" form=quote-generator onclick="validForm()">
答案 1 :(得分:0)
ReferenceError:未定义formQuote
<input type=submit value="Submit" form=quote-generator onclick="validForm()">