js中获取表单对象的方法 <input type=”text” />
HTML部分
<form action="" name="form1" id="frm1" method="post">
<input type="text" value="请输入用户名" name="number" id="number" ><br />
<input type="button" value="提交" onclick="checkForm()">
</form>
JS部分
方法1:
<script> funciton checkForm(){ var obj=document.getElementById("number"); alert(obj.value) } </script>
方法2:
<form id ="form1" action="test.do" method="post">
<input type="text" name="a" value="1">
<br>
<input type="text" name="a" value="2">
<br>
<input type="text" name="a" value="3">
<br>
<input type="submit" value="提交">
</form>
</body>
<script type="text/javascript">
document.getElementById('form1').onsubmit=function(){
var as=document.getElementsByName('a'); //数组
for(i=0;i<as.length;i++){
alert(as[i].value);
}
}
</script>
方法3:
<script> funciton checkForm(){ var obj=document.form1.number.value; var obj=document.forms[0].number.value; var obj=document.forms[0]['number'].value; var obj=document.form1['number'].value; } </script>