我要利用一个图形界面来生成一条SQL语句,全部代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
 function dispose(){
var str="";
var ostr="";
var wstr="";
    var fstr="";
var ostr_1=""
    
 if(document.form1.zdname.length){ 
    for(var i=0; i<document.form1.zdname.length;i++){  //得到所选择字段的个数
  if(document.form1.zdname[i].checked){   //选择了一个以上的字段
      str += document.form1.zdname[i].value + ",";  //将所有字段用“,”号连接
  }
  else{
      if(document.form1.zdname.checked){  //只选择了一个字段
    str = document.form1.zdname.value;  
  }
  }
}
if(str.lastIndexOf(",") == str.length -1){  //删除最后一个字段后面的“,”
     str=str.substring(0, str.length - 1)
}
   if(str != "") str="select "+str+" from user"; else str="select * from user";  //如果没有选择字段,SQL语句为select * from user
                                                                                 //如果选择了字段,SQL语句为select [所选择字段名] from user
  }//*******************************************************************************************
//*******************************************************************************************      if(document.form1.clname.length){
     for(var j=0; j<document.form1.clname.length;j++){
       
  if(document.form1.clname[j].checked){
     
 ostr_1 += document.form1.clname[j].value + " " + document.form1.order_0.value + ",";
  }
  else{
     if (document.form1.clname.checked)
     {
   ostr_1 = document.form1.clname.value + " " + document.form1.order_0.value;
     }
  }
 }
         if (ostr_1.lastIndexOf(",") == ostr_1.length - 1)
         {
    ostr_1=ostr_1.substring(0, ostr_1.length -1);
         }
 if(ostr_1 != "") ostr=" order by "+ostr_1; else ostr="";
   }
   document.form1.sqlshow.value=str+ostr;
}
//-->
</SCRIPT>
</head><body>
<form id="form1" name="form1" method="post" action="">
  <label> </label>
  <table width="500" height="120" border="1" cellpadding="0" cellspacing="0">
    <tr>
      <td height="30" colspan="4"><div align="left">选择要查询的字段:</div></td>
    </tr>
    <tr>
      <td height="30" colspan="4"><div align="left">
        <label>
        <input name="zdname" type="checkbox" id="zdname" value="username" />
        </label>
      username 
      <label>
      <input name="zdname" type="checkbox" id="zdname" value="password" />
      </label>
      password</div></td>
    </tr>
    <tr>
      <td width="25%" height="30"><div align="center">
        <label>
        <input name="clname" type="checkbox" id="clname" value="username" />
        </label>
      username</div></td>
      <td width="30%" height="30"><div align="center">
        <label>
        <select name="order_0" id="order_0">
  <option></option>
          <option value="ASC">升序</option>
          <option value="DESC">降序</option>
        </select>
        </label>
      </div></td>
      <td width="20%" height="30"><div align="center">
        <label></label>
        <label>
        <input name="where_0" type="text" id="where_0" size="6" />
        </label>
      </div></td>
      <td width="25%" height="30"><div align="center">
        <label>
        <input name="values_0" type="text" id="values_0" size="15" />
        </label>
      </div></td>
    </tr>
<tr>
      <td width="25%" height="30"><div align="center">
        <label>
        <input name="clname" type="checkbox" id="clname" value="password" />
        </label>
      password</div></td>
      <td width="30%" height="30"><div align="center">
        <label>
        <select name="order_1" id="order_1">
  <option></option>
          <option value="ASC">升序</option>
          <option value="DESC">降序</option>
        </select>
        </label>
      </div></td>
      <td width="20%" height="30"><div align="center">
        <label>
        <input name="where_1" type="text" id="where_1" size="6" />
        </label>
      </div></td>
      <td width="25%" height="30"><div align="center">
        <label>
        <input name="values_1" type="text" id="values_1" size="15" />
        </label>
      </div></td>
    </tr>
<tr>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
      <td height="30"><div align="center">
        <label>
        <input type="button" name="Submit" value="生成" onClick="dispose()" />
        </label>
      </div></td>
    </tr>
  </table>
  <label><br />
  <br />
  <textarea name="sqlshow" cols="80" rows="10" id="sqlshow" readonly="readonly"></textarea>
  </label>
</form>
</body>
</html>但出现了问题,就是我用下拉列表(order_0、order_1……)的值来实现升序(ASC)和降序(DESC),在JS脚本中是通过第41和46行的document.form1.order_0.value来获取这个列表值的,但这样如果给他写死了,就只能实现一个效果,要么全部字段生序,要么降序,请问怎么写才能将他们分开,来各自实现各自的升、降序呢?

解决方案 »

  1.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
     function dispose(){
    var str="";
    var ostr="";
    var wstr="";
        var fstr="";
    var ostr_1=""
        
     if(document.form1.zdname.length){ 
        for(var i=0; i<document.form1.zdname.length;i++){  //得到所选择字段的个数
      if(document.form1.zdname[i].checked){   //选择了一个以上的字段
          str += document.form1.zdname[i].value + ",";  //将所有字段用“,”号连接
      }
      else{
          if(document.form1.zdname.checked){  //只选择了一个字段
        str = document.form1.zdname.value;  
      }
      }
    }
    if(str.lastIndexOf(",") == str.length -1){  //删除最后一个字段后面的“,”
         str=str.substring(0, str.length - 1)
    }
       if(str != "") str="select "+str+" from user"; else str="select * from user";  //如果没有选择字段,SQL语句为select * from user
                                                                                     //如果选择了字段,SQL语句为select [所选择字段名] from user
      }//*******************************************************************************************
    //*******************************************************************************************      if(document.form1.clname.length){
         for(var j=0; j<document.form1.clname.length;j++){
           
      if(document.form1.clname[j].checked){
         document.form1.clname[j].id.match(/_(\d{1,})/g);
     ostr_1 += document.form1.clname[j].value + " " + document.form1["order_" + RegExp.$1].value + ",";
      }
      else{
         if (document.form1.clname.checked)
         {
          document.form1.clname.id.match(/_(\d{1,})/g);
       ostr_1 = document.form1.clname.value + " " + document.form1["order_" + RegExp.$1].value;
         }
      }
     }
             if (ostr_1.lastIndexOf(",") == ostr_1.length - 1)
             {
        ostr_1=ostr_1.substring(0, ostr_1.length -1);
             }
     if(ostr_1 != "") ostr=" order by "+ostr_1; else ostr="";
       }
       document.form1.sqlshow.value=str+ostr;
    }
    //-->
    </SCRIPT>
    </head><body>
    <form id="form1" name="form1" method="post" action="">
      <label> </label>
      <table width="500" height="120" border="1" cellpadding="0" cellspacing="0">
        <tr>
          <td height="30" colspan="4"><div align="left">选择要查询的字段:</div></td>
        </tr>
        <tr>
          <td height="30" colspan="4"><div align="left">
            <label>
            <input name="zdname" type="checkbox" id="zdname_0" value="username" />
            </label>username
            <label>
          <input name="zdname" type="checkbox" id="zdname_1" value="password" />
          </label>
          password</div></td>
        </tr>
        <tr>
          <td width="25%" height="30"><div align="center">
            <label>
            <input name="clname" type="checkbox" id="clname_0" value="username" />
            </label>
          username</div></td>
          <td width="30%" height="30"><div align="center">
            <label>
            <select name="order_0" id="order_0">
      <option></option>
              <option value="ASC">升序</option>
              <option value="DESC">降序</option>
            </select>
            </label>
          </div></td>
          <td width="20%" height="30"><div align="center">
            <label></label>
            <label>
            <input name="where_0" type="text" id="where_0" size="6" />
            </label>
          </div></td>
          <td width="25%" height="30"><div align="center">
            <label>
            <input name="values_0" type="text" id="values_0" size="15" />
            </label>
          </div></td>
        </tr>
    <tr>
          <td width="25%" height="30"><div align="center">
            <label>
            <input name="clname" type="checkbox" id="clname_1" value="password" />
            </label>
          password</div></td>
          <td width="30%" height="30"><div align="center">
            <label>
            <select name="order_1" id="order_1">
      <option></option>
              <option value="ASC">升序</option>
              <option value="DESC">降序</option>
            </select>
            </label>
          </div></td>
          <td width="20%" height="30"><div align="center">
            <label>
            <input name="where_1" type="text" id="where_1" size="6" />
            </label>
          </div></td>
          <td width="25%" height="30"><div align="center">
            <label>
            <input name="values_1" type="text" id="values_1" size="15" />
            </label>
          </div></td>
        </tr>
    <tr>
          <td height="30">&nbsp;</td>
          <td height="30">&nbsp;</td>
          <td height="30">&nbsp;</td>
          <td height="30"><div align="center">
            <label>
            <input type="button" name="Submit" value="生成" onClick="dispose()" />
            </label>
          </div></td>
        </tr>
      </table>
      <label><br />
      <br />
      <textarea name="sqlshow" cols="80" rows="10" id="sqlshow" readonly="readonly"></textarea>
      </label>
    </form>
    </body>
    </html>