我不想用
if(document.form1.text1.value=="")
{
alert("请输入姓名!");
document.form1.text1.focus();
return false;
}
这样判断一个的话 要写很多次我想用变量循环,把变量带进去注意: 不是所有的元素.. 是我指定的那几个即可..
譬如 text1 text3 text5 这样的..谢谢了..
if(document.form1.text1.value=="")
{
alert("请输入姓名!");
document.form1.text1.focus();
return false;
}
这样判断一个的话 要写很多次我想用变量循环,把变量带进去注意: 不是所有的元素.. 是我指定的那几个即可..
譬如 text1 text3 text5 这样的..谢谢了..
for(i=0,i<5,++i)
{
if(document.getElementById("text"+i).value=="")
alert(请填入所有必选信息);
}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> new document </title>
<meta name="keywords" content="">
<meta name="description" content="">
</head> <body>
<script type="text/javascript">
<!--
function kk () {
var arrName = ["text1","text3"];
var arrCnName = ["姓名","籍贯"];
var objTemp;
for (var i=0; i<arrName.length; i++) {
eval("objTemp = document.form1."+arrName[i]);
if (objTemp.value=="") {
alert("请输入"+arrCnName[i]+"!");
objTemp.focus();
return false;
}
}
} //-->
</script>
<form method="post" action="" name="form1" onsubmit="return kk()">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="text" name="text3">
<input type="submit"/>
</form>
</body>
</html>
function check(arr){
var _return = true;
for(var i in arr){
var obj = document.getElementById(i);
if(!obj.value){
_return = false;
alert("请输入" + arr[i]);
obj.focus();
break;
}
}
return _return;
}function checkForm(){
var arr = new Array();
arr["username"] = "用户名";
arr["password"] = "密码";
return check(arr);
}
</script>
<form onsubmit="return checkForm()">
<input type="text" id="username" name="username">
<input type="text" id="password" name="password">
<input type="submit" value="submit">
</form>
大体这样