<form action="" method="post" onSubmit="return inputNull(this)">
<table>
<p>
<span>姓名:</span>
<input type="text" placeholder="姓名">
</p>
<p>
<span>年龄:</span>
<input type="text" placeholder="年龄">
</p>
<input type="submit" value="登录">
</form>
<script type="text/javascript">
function inputNull(form){
for(i=0;i<form.length;i++){
//form属性的elements的首字母e要小写
if(form.elements[i].value == ""){
alert("亲:" + form.elements[i].placeholder + "不能为空");
form.elements[i].focus();
return false;
}
}
}
</script>
效果: