伙计们帮助....为什么错误信息不显示
问题描述:
<html>
<head>
<script type="text/javascript">
function validate() {
var a = document.form.name.value;
if(!(a)) {
document.getElementById("errorBox").innerHTML = "*Please fill required";
} else {
document.getElementById("errorBox").innerHTML = "";
}
}
</script>
</head>
<body>
<form method= "post" name="form" onsubmit="return validate();">
Name :<input type="text" id="name" name="name" />
<div id="errorBox"> </div> <br>
<input type="submit" name="submit" value="submit" />
<input type="Reset" name="Reset" />
</body>
不知道为什么错误信息不显示?请帮助我。伙计们帮助....为什么错误信息不显示
答
替换您如下代码
var a = document.form.name.value;
if(a == "")
{
document.getElementById("errorBox").innerHTML = "*Please fill required";
}
与此代码
var a = document.getElementById("name").value;
if(!(a))
{
document.getElementById("errorBox").innerHTML = "*Please fill required";
}
如果仍然发行不解决,请告诉我们UR的HTML代码,以便我们检查是否有任何错误或没有。
答
在javascript中,空字符串与null
值或undefined
不相同,但它们在条件下都解析为false!
试试这个:
if(!a){
// your code
}
看一看这个捣鼓一个有效的解决方案:https://jsfiddle.net/wvofbjdk/
如果它不能在你的代码工作,那么错误是在其他地方。查看控制台是否有错误(请参阅此链接以开始使用chrome:https://developers.google.com/web/tools/chrome-devtools/debug/console/?hl=en)。
请尝试把断点,看看什么值的一个和发生什么 – chenop
锄头把断点chenop? – prathap
这是一个很好的基本问题 - 请检查此问题 http://stackoverflow.com/questions/66420/how-do-you-launch-the-javascript-debugger-in-google-chrome – chenop