onsubmit getElementById getElementByName

来源:互联网 发布:南京家庭网络布线 编辑:程序博客网 时间:2024/04/30 15:33
<script type="text/javascript">    function check(){    var username = document.getElementById("username").value;    if(username==null||username==""){    alert("用户名不能为空!");    return false;    }    }</script></head><body><h1>estore用户注册</h1><form action="/RegistServlet" method="post" onsubmit="return check();"><table><tr><td>用户名</td><td><input type="text" name="username" id="username"/></td></tr><tr><td><input type="submit" value="注册"></td><td><input type="reset" value="重置"></td></tr></table></form>

1:onsubmit="return check();" 必须有return,否则表单无论如何都会提交,onsubmit默认返回TRUE

2:ID为唯一属性,不能重复,name则可以重复,所以

getElementById获得的是唯一的值 

getElementByName获得的是数组,即使表单中只有唯一的name属性

0 0
原创粉丝点击