<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="book" uri="http://xiao.sms.show.Show"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style>
.cs1{
display:"none"
}
</style>
<script >
var btn1=document.getElementById("btn1");
var ipts=document.getElementByClassName("cs1");
alter(ipts);
btn1.onclick=function (){
alter("error")
for(var i=0;i<ipts.length;i++){
ipts[i].style.display="display"
}
function check(){
for(var i=0;i<ipts.length;i++){
if(ipts[i].value!==null){
return false;
}
}
return true;
}
</script>
</head>
<body>
<form action="CommunionDB/servlet/Add" onsubmit="return check()">
<table>
<book:oper>g</book:oper>
</table>
<input type="button" name="add" value="add" id="btn1"><br/>
<input type="text" name="bName" class="cs1"><br/>
<input type="text" name="autor"class="cs1"><br/>
<input type="text" name="price" class="cs1"><br/>
<input type="submit" value="make sure to add" class="cs1">
</form>
</body>
</html>
一堆问题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<form action="CommunionDB/servlet/Add" onsubmit="return check()">
<table>
<book:oper>g</book:oper>
</table>
<input type="button" name="add" value="add" id="btn1"><br />
<input type="text" name="bName" class="cs1"><br />
<input type="text" name="autor" class="cs1"><br />
<input type="text" name="price" class="cs1"><br />
<input type="submit" value="make sure to add" class="cs1">
</form>
<script>
//放下面来,要不dom没有生成,要么放到window.onload里面,check不能放里面
var btn1=document.getElementById("btn1");
//var ipts = document.getElementByClassName("cs1");
//========>
var ipts = document.getElementsByClassName("cs1");
//alter(ipts);
//========>
alert(ipts);
btn1.onclick = function () {
//alter("error")
//========>
alert("error")
for (var i = 0; i < ipts.length; i++) {
//ipts[i].style.display = "display"
ipts[i].style.display = "inline"
}
}////////////////////少了
function check() {
for (var i = 0; i < ipts.length; i++) {
//if (ipts[i].value !== null) {
if (ipts[i].value =='') {
return false;
}
}
return true;
}
</script>
如果有问题,可以看下F12的报错,另外建议学习一下jQuery.