js中用alert没有显示提示。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function checkForm(){
var input1 = document.getElementById("username");
var uValue = input1.value;
if(uValue.length >= 6){
return true;
}else{
alert("对不起,用户名太短啦!")
}
return false;
}
</script>

</head>
<body>
<form action="../01-网站首页的优化/网站首页.html" onsubmit="checkForm" >
用户名:<input type="text"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

第1个回答  2019-05-16
Js代码没问题,是html标签有两处错误。
onsubmit="checkForm()"函数名后面少了括号
用户名:<input id="username" type="text"/><br/>
id属性你没有写。
改了这两处就可以弹出alert了。我已经测试过。本回答被提问者采纳
第2个回答  2019-05-16

    return false;这个放在alert("对不起,用户名太短啦!")下面,注意放在大括号里面

2.alert("对不起,用户名太短啦!")后面缺少;号

3.<input type="text"/>修改成<input type="text" id="username"/>

第3个回答  2019-05-16
你没设置ID为userName,获得不到DOM和value,调用函数没有加括号。
第4个回答  2019-05-16

第5个回答  2019-05-16
<form action="../01-网站首页的优化/网站首页.html" onsubmit="return checkForm()" >
用户名:<input type="text" id="username"/><br/>
<input type="submit" value="提交"/>
</form>
相似回答