javascript实例--循环判断查询表单,构建SQL语句

来源:互联网 发布:网络推手收费 编辑:程序博客网 时间:2024/05/22 15:06

 

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<!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=GB18030">
<script language="javascript">
    
function subm(obj){
        
var list=new Array("name","grade","peciality","course","level","form");
        
var j=0;
        
var sql="select * from cjcj ";
        
var keyword="where "
        
for(i=0;i<list.length;i++){
            
var selectObj=document.getElementById("what"+list[i]);
            
var selectValue=selectObj.options[selectObj.selectedIndex].value;
            
if(selectValue!="all"+list[i]){
                
var valueInput=document.getElementById(list[i]+"KeyWord");
                
if(valueInput.value==""){
                    alert(selectObj.options[selectObj.selectedIndex].text);
                    
return false;
                }
else{
                    keyword
+=" "+selectValue+"='"+valueInput.value+"' and";
                }

            }
else{
                j
++;    
            }

        }

        
if(j==6){
            alert(
"请输入一个关键字")
            
return false;
        }

        obj.sql.value
=sql+keyword.substring(0,keyword.length-3);
        
return true;
    }

</script>
</head>
<body>
<center><input type="button" value="录入成人教育成绩"
    onclick
="document.location.href='./addcjcj.jsp'"></center><br>
<form action="./body/zksearchbody.jsp" method="post" onsubmit="return subm(this)">
<table border="1" width="80%" align="center">
    
<tr>
        
<td colspan="2" align="center">成人教育成绩查询</td>
    
</tr>
    
<tr>
        
<td align="center" width="30%"><select style="width:80%"
            id
="whatname" name="whatname">
            
<option selected value="allname">查询所有学生成绩</option>
            
<option value="name">请输入姓名</option>
            
<option value="xh">请输入学号</option>
        
</select></td>
        
<td><input type="text" style="width:90%" name="nameKeyWord">
        
</td>
    
</tr>
    
<tr>
        
<td align="center"><select style="width:80%"   id="whatgrade" name="whatgrade">
            
<option value="allgrade">查询所有年级成绩</option>
            
<option value="grade">请输入年级</option>
        
</select></td>
        
<td><input type="text" style="width:90%" name="gradeKeyWord">
        
</td>
    
</tr>
    
<tr>
        
<td align="center"><select style="width:80%" id="whatpeciality" name="whatpeciality">
            
<option value="allpeciality">查询所有专业成绩</option>
            
<option value="peciality">请输入专业名称</option>
        
</select></td>
        
<td><input type="text" style="width:90%" name="pecialityKeyWord">
        
</td>
    
</tr>
    
<tr>
        
<td align="center"><select style="width:80%" id="whatcourse" name="whatcourse">
            
<option value="allcourse">查询所有课程成绩</option>
            
<option value="courseid">请输入课程编码</option>
            
<option value="coursename">请输入课程名称</option>
        
</select></td>
        
<td><input type="text" style="width:90%" name="courseKeyWord">
        
</td>
    
</tr>
    
<tr>
        
<td align="center"><select style="width:80%" id="whatlevel" name="whatlevel">
            
<option value="alllevel">查询所有层次成绩</option>
            
<option value="level">请输入层次</option>
        
</select></td>
        
<td><input type="text" style="width:90%" name="levelKeyWord">
        
</td>
    
</tr>
    
<tr>
        
<td align="center"><select style="width:80%" id="whatform" name="whatform">
            
<option value="allform">查询所有形式成绩</option>
            
<option value="form">请输入形式</option>
        
</select></td>
        
<td><input type="text" style="width:90%" name="formKeyWord">
        
</td>
    
</tr>
    
<tr>
        
<td align="center" colspan="2"><input type="Submit" name="Submit"
            value
="提交查询"></td>
    
</tr>
    
</table>
<input type="text" name="sql" id="sql">
</form>

</body>
</html>

 

 

 

原创粉丝点击