<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="http://www.baidu.com">
username:<input name="username"/><br>
password:<input name="password"/><br>
<input type="submit">
</form>
</body>
<script type="text/javascript">
var submit1 = document.getElementsByTagName("input")[2];
submit1.onclick = function() {
var username = document.getElementsByTagName("input")[0];
if(username.value != null) {
return true;
}else {
return false;
}
}
</script>
</html>不管我 username 里面有没有填入字符, 按提交 都提交到 www.baidu.com了! 不是为空不提交的么! 难道不添任何东西 input /text 里面的默认值不为空么?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="http://www.baidu.com">
username:<input name="username"/><br>
password:<input name="password"/><br>
<input type="submit">
</form>
</body>
<script type="text/javascript">
var submit1 = document.getElementsByTagName("input")[2];
submit1.onclick = function() {
var username = document.getElementsByTagName("input")[0];
if(username.value != null) {
return true;
}else {
return false;
}
}
</script>
</html>不管我 username 里面有没有填入字符, 按提交 都提交到 www.baidu.com了! 不是为空不提交的么! 难道不添任何东西 input /text 里面的默认值不为空么?
鉴定完毕
改进一下
username.value.replace(/^\s+|\s+$/g,"") == ""
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
<title>Untitled Document </title>
</head>
<body>
<form action="http://www.baidu.com"> username: <input type="text" name="username"/> <br>
<input type="submit" onclick="return check()">
</form>
</body>
<script type="text/javascript">
function check(){
if(document.getElementsByName("username")[0].value==""){
alert("用户名不能为空");
return false;
}
return true;
}
</script>
</html>
给你修改了下 去试试