[英]JavaScript: .innerhtml error message form validation
我正在尝试使用.innerhtml在按钮上方输入错误消息。 我认为我已经接近将其投入使用,但是我遇到了麻烦。 我认为这是一个非常简单的语义错误,因为我是JS新手。
这是我的jsfiddle: http : //jsfiddle.net/ajX2U/
var sel = document.getElementById("stateType");
switch(sel.value) {
case "VA":
location.href = "http://www.test1.com";
break;
case "MD":
location.href = "http://www.test2.com";
break;
case "IL":
location.href = "http://www.test3.com";
break;
case "TX":
location.href = "http://www.test4.com";
break;
case "other":
location.href = "http://www.test5.com";
break;
// oh dear we have selected a wrong option
default:
document.getElementById('error').innerHTML = 'Fred Flinstone';
}
}
在您的提琴中,您只是忘了包括功能头。 添加之后,它对我有用。
function validate(){
var sel = document.getElementById("stateType");
switch(sel.value) {
case "VA":
location.href = "http://www.test1.com";
break;
case "MD":
location.href = "http://www.test2.com";
break;
case "IL":
location.href = "http://www.test3.com";
break;
case "TX":
location.href = "http://www.test4.com";
break;
case "other":
location.href = "http://www.test5.com";
break;
// oh dear we have selected a wrong option
default:
document.getElementById('error').innerHTML = 'Fred Flinstone';
}
}
从您的小提琴中,这就是问题所在。
(使用Chrome并打开控制台,您将看到它)
<script type='text/javascript'>//<![CDATA[
window.addEvent('load', function() {
var sel = document.getElementById("stateType");
switch(sel.value) {
case "VA":
location.href = "http://www.test1.com";
break;
case "MD":
location.href = "http://www.test2.com";
break;
case "IL":
location.href = "http://www.test3.com";
break;
case "TX":
location.href = "http://www.test4.com";
break;
case "other":
location.href = "http://www.test5.com";
break;
// oh dear we have selected a wrong option
default:
document.getElementById('error').innerHTML = 'Fred Flinstone';
}
}
var sel = document.getElementById("stateType");
});//]]>
变量sel放置在错误的位置并将其断开(无论如何,第二个var sel)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.