首页 > 代码库 > DOM操作表单

DOM操作表单

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title></title>

<script>
function getFormInformation(){
var myform = document.forms[0];
alert(myform.id+myform.method+myform.target);
myform.submit(); //提交表单
}
</script>
</head>

<body>
<form id="testform" action="http://www.baidu.com" method="get" target="_blank">
<input name="username" type="text" value="http://www.mamicode.com/请输入密码" /><br />
<input name="pwd" type="password" value="http://www.mamicode.com/123" /><br />
<select name="city">
<option value="http://www.mamicode.com/CD">成都</option>
<option value="http://www.mamicode.com/SH" selected="selected">上海</option>
</select><br />
<input type="button" value="http://www.mamicode.com/Get number one" onclick="var n = document.getElementById(‘testform‘).elements[0].value;alert(n);"/><br />
<input type="button" value="http://www.mamicode.com/Get number two" onclick="alert(document.getElementById(‘testform‘).elements[‘pwd‘].value);"/><br />
<input type="button" value="http://www.mamicode.com/Get number three" onclick="alert(document.getElementById(‘testform‘).city.value);"/><br />
<input type="button" value="http://www.mamicode.com/Get more information" onclick="getFormInformation()"/>
</form>
</body>


</html>

DOM操作表单