为什么提交验证一直是失败的

山中习静观朝槿 2017-07-19 02:35:42
为什么下面的代码在验证的时候,不会出现警告框,只是会直接跳转
<!doctype html>
<html>
<head >
<script>
function checkform(){
var name=document.getElementById("none_name")
if (name.value==null&&name.value==""){
alert("请填写名字!")
return false;
}
</script>
<body>
<form action="submitpage.htm" onsubmit="return checkform()" method="post">
姓名: <input type="text" id="none_name">
<input type="submit" value="验证">
</form>
</body>
</html>
...全文
197 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 6 楼 gumeng812 的回复:
[quote=引用 5 楼 gumeng812 的回复:] 楼上说反了,if外边没有return的话,默认返回true。

<!doctype html>
<html>
<head >
	<script>
        function checkform() {
            var name = document.getElementById("none_name");
            if (name.value != "") {
                alert("请填写名字!");
                return false;
            };
        };
	</script>
<body>
<form  action="submitpage.htm" onsubmit="return checkform()" method="post">
	姓名: <input type="text" id="none_name">
	<input type="submit" value="验证">
</form>
</body>
</html>
啊呸! 测试代码,发错了,if里边写(name.value == "")大意了[/quote]嗯呢了解了,以后得多注意细节,谢啦
_林小夕 2017-07-19
  • 打赏
  • 举报
回复
引用 5 楼 gumeng812 的回复:
楼上说反了,if外边没有return的话,默认返回true。

<!doctype html>
<html>
<head >
	<script>
        function checkform() {
            var name = document.getElementById("none_name");
            if (name.value != "") {
                alert("请填写名字!");
                return false;
            };
        };
	</script>
<body>
<form  action="submitpage.htm" onsubmit="return checkform()" method="post">
	姓名: <input type="text" id="none_name">
	<input type="submit" value="验证">
</form>
</body>
</html>
啊呸! 测试代码,发错了,if里边写(name.value == "")大意了
_林小夕 2017-07-19
  • 打赏
  • 举报
回复
楼上说反了,if外边没有return的话,默认返回true。

<!doctype html>
<html>
<head >
	<script>
        function checkform() {
            var name = document.getElementById("none_name");
            if (name.value != "") {
                alert("请填写名字!");
                return false;
            };
        };
	</script>
<body>
<form  action="submitpage.htm" onsubmit="return checkform()" method="post">
	姓名: <input type="text" id="none_name">
	<input type="submit" value="验证">
</form>
</body>
</html>
_林小夕 2017-07-19
  • 打赏
  • 举报
回复
首先不说技术哈,你这代码function花括号没闭合,变量声明后和alert后没有分号。
jio可 2017-07-19
  • 打赏
  • 举报
回复
判断也是问题。既要等于null还必须等于""
jio可 2017-07-19
  • 打赏
  • 举报
回复
引用 1 楼 qq_37243045 的回复:
大牛请指教~
return写在判断里面了,如果满足if条件外面没有return默认的返回值就是false.所以一直都是失败.
  • 打赏
  • 举报
回复
大牛请指教~

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧