无法获取属性value的值,对象为null或未定义,怎么解决啊?
获取为空,利用debug调试,找到第一次值传递为空的地方,重新查看那个地方使用的方法即可。
具体操作:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function baocun() {
//id
var addnumber1 = document.getElementsByName("myid");
alert(addnumber1[0].value);
var addnumber2 = document.getElementById("myid").value;
//名字
var addname1 = document.getElementsByName("myname");
alert(addname1[0].value);
var addname2 = document.getElementById("myname").value;
//学校
var addschool1 = document.getElementsByName("myschool");
var addschool2 = document.getElementById("myschool").value;
//邮箱
var addemail1 = document.getElementsByName("myemail");
var addemail2 = document.getElementById("myemail").value;
if (addnumber2 == null || addnumber2 == "") {
alert("学号不能空!");
} else if (addname2 == null || addname2 == "") {
alert("姓名不能空!");
} else if (addschool2 == null || addschool2 == "") {
alert("学校不能空!");
} else {
document.getElementById("id").value = addnumber1[0].value;
document.getElementById("nameid").value = addname1[0].value;
document.getElementById("schoolid").value = addschool1[0].value;
document.getElementById("emailid").value = addemail1[0].value;
tijiao.submit();
location.href = "Info.jsp";
}
}
function no() {
location.href = "Info.jsp";
}
</script>
<body>
<center>
<h2>----页面表单提交小程序----</h2>
<table width="600">
<tr>
<td>
<h4>添加页面</h4>
</td>
</tr>
</table>
<form action="AddServelet" method="post" name="tijiao">
<input type="hidden" name="addid" value="" id="id"> <input
type="hidden" name="addname" value="" id="nameid"> <input
type="hidden" name="addschool" value="" id="schoolid"> <input
type="hidden" name="addemail" value="" id="emailid">
</form>
<table border="1" width="600">
<tr>
<th width="300"> 学 号 </th>
<th width="300"> 姓 名 </th>
<th width="1000"> 学 校 </th>
<th width="1000"> 邮 箱 </th>
</tr>
<tr>
<td><input type="text" id="myid" name="myid"
onKeypress="return (/[\d.]/.test(String.fromCharCode(event.keyCode)))"
style="ime-mode: Disabled" title="只能输入数字" />
</td>
<td><input type="text" name="myname" id="myname" />
</td>
<td><input type="text" name="myschool" id="myschool" />
</td>
<td><input type="text" name="myemail" id="myemail" />
</td>
</tr>
</table>
<p />
<button type="button" onclick="baocun();"> 保 存 </button>
<button type="button" onclick="no();"> 取 消 </button>
</center>
<!-- 添加功能 -->
</body>
</html>