为什么以下代码不起作用?
只要我点击提交按钮,它就会刷新页面
我刚开始学习JavaScript,所以如果你有任何建议来提高我对JS的了解,也可以随意分享。
<html>
<head>
<title>Form </title>
<script src="form.js"></script>
</head>
<body>
<form id="Form" onsubmit="return handleSubmit()">
<input id="username" type="text" name="username ">
<input id="password" type="password" name="password">
<input id="submit" type="submit" name="Login">
</form>
</body>
</html>
function handleSubmit(){
console.log("Hi");
var form = document.getElementById("Form");
console.log("va="+form.childern[0].value);
var u = form.childern[0].value;
if (form.childern[0].length==0 && isNaN(typeOf(u))==true){
alert("Enter Correct username");
} else {
console.log("username = " +u);
}
return false;
}
答案 0 :(得分:1)
这是一个正确的代码伙伴。只是js的一个小变化。
function handleSubmit(){
console.log("Hi");
var form = document.getElementById("Form");
console.log("va="+form.children[0].value);
var u = form.children[0].value;
var a=typeof u;
if (form.children[0].value.length!=0 &&isNaN(u))
{
console.log("username = " +u);
} else {
alert("Enter Correct username");
}
return false;
}
&#13;
<html>
<head>
<title>Form </title>
<script src="form.js"></script>
</head>
<body>
<form id="Form" onsubmit="return handleSubmit()">
<input id="username" type="text" name="username ">
<input id="password" type="password" name="password">
<input id="submit" type="submit" name="Login">
</form>
</body>
</html>
&#13;
答案 1 :(得分:0)
我会在检测到它们时添加更多信息!希望这暂时有用!