错误:无法获取属性value的值,对象为null或未定义,怎么解决啊? 20
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<style type="text/css">
</style>
<script type="text/javascript">
<!--
function $(id){
return document.getElementById("id");
}
function checkName()
{
var myna=$("mytext").value;
var mynalen=myna.length;
var firstchar=myna.charAt(0);
if(mynalen<5 || mynalen>20)
{
alert("用户名长度不规范化!");
$("err_name").innerHTML="用户名长度不规范化!";
}else if (firstchar>="0"&& firstchar<="9")
{
$("err_name").innerHTML="用户名首字符有不规范化!";
}
}
function checkPsd1(){
var mina=$("mypsd1").value;
var mimalen=mima.length;
if (mimalen<5||miam>20)
{
$("err_psd1").innerHTML="密码长度不规范";
}else if (mims.length==""||mima==null)
{
$("err_psd1").innerHTML="密码不能为空";
}
}
function cheakPsd2(){
}
function cheakPsd1(){
}
//-->
</script>
</head>
<body>
<div id="" class="">
<form method="post" action="">
<table border="1px" color="bule" width="600px" height="220px">
<caption>表单验证</caption>
<tr>
<td>用户名</td>
<td><input type="text" name="" id="mytext" onblur="checkName();"></td>
<td><label id="err_name"></label></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="" id="mypsd1" onblur="checkPsd1();"></td>
<td><label id="err_psd1"></label></td>
</tr>
<tr>
<td>确认密码 </td>
<td><input type="password" name="" id="mypsd2" onblur="checkPsd2();"></td>
<td><label id="err_psd2"></label></td>
</tr>
<tr>
<td>电子邮件</td>
<td><input type="text" name="" id="myemail" onblur="checkEmail();"></td>
<td><label id="err_email"></label></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="提交" onclick="">
<input type="reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html> 展开
获取为空,利用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>
<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>