日期:2014-05-17  浏览次数:20808 次

更新数据库,如果name0/name1/name2为空时,就不进入更新页面,要怎么做啊
更新数据库,如果name0/name1/name2为空时,就不进入更新页面,要怎么做啊[b][/b][size=18px][/size]

<%@page import="java.util.List"%>
<%@page import="cn.com.chengang.InsertAndSelect"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Update title here</title>
<% String id=request.getParameter("id");
String sql="select * from test_table where id="+id;
List<String> vals = new InsertAndSelect().query(sql);
String name0="";
String name1="";
String name2="";

if(vals!=null&&vals.size()==1){
String val = vals.get(0);
String[] strs = val.split("_");
name0=strs[1];
name1=strs[2];
name2=strs[3];
   
}
%>
<script type="text/javascript">
function update(id){
alert(id);
var name0=document.getElementById("name0").value;
var name1=document.getElementById("name1").value;
var name2=document.getElementById("name2").value;

document.location.href="firstmysql.jsp?action=update&name0="+name0+"&name1="+name1+"&name2="+name2+"&id="+id;
 

}
</script>


</head>
<body>
<table>
<tr>
<td>name0:</td>
<td><input name="name0" id="name0" value="<%=name0 %>"></td>
</tr>
<tr>
<td>name1:</td>
<td><input name="name1" id="name1" value="<%=name1 %>"></td>
</tr>
<tr>
<td>name2:</td>
<td><input name="name2" id="name2" value="<%=name2 %>"></td>
</tr>
<tr>
<td><input type="button" value="submit" onclick="update('<%=id%>')"> </td><td><input type="reset"> </td>
</tr>
</table>
</body>
</html>

------解决方案--------------------
居然不是用的form表单,而是拼字符串。。。

为这句话:
document.location.href="firstmysql.jsp?action=update&name0="+name0+"&name1="+name1+"&name2="+name2+"&id="+id;

增加判断就好了(不考虑去空格的情况下):
if (name0 && name1 && name2) {
document.location.href="firstmysql.jsp?action=update&name0="+name0+"&name1="+name1+"&name2="+name2+"&id="+id;
} else {
alert("XXXOOO 不能为空");
}


另外也可以专门写个函数来判断是否为空,比如:
function isEmpty(str) {
if (str) {
if (name0.replace(/\s+/g,"").length() > 0) return false;
}
return true;
}