本文實例講述了JavaScript生成SQL查詢表單的方法。分享給大家供大家參考。具體如下:
這里使用JavaScript生成復雜的SQL查詢表單,運行一下就明白了,它可以根據選擇的查詢條件,自動修改你的SQL語句,是一個很典型的應用。
運行效果截圖如下:

具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>查詢條件表單</title><style>*{ font-size:12px; padding:0; margin:0;}body{ padding:40px;}#MainBox{ border:#666 1px solid; background-color:#eee; width:700px;}#MainBox td{ padding:4px;}#ConditionBox{ height:150px; width:100%; overflow-y:auto; border:#bbb 1px solid; padding:2px; background-color:#fff;}.tmFrame{ border:#eee 1px solid; padding:2px; width:100%;}.tmFrame_highlight{ border:#666 1px solid; padding:2px; width:100%; background-color:#f7f7f7;}.fname{ float:left; width:200px;}.conn{ float:left; width:100px;}.fvalue{ float:left; width:100px;}.handlebox{ float:right; width:180px; display:none;}.handlebox_view{ float:right; width:180px; display:block;}.rbox{ float:right; margin:1px; background-color:#999; color:#fff; padding:1px; width:15px; cursor:hand;}legend{ border:#bbb 1px solid; padding:4px;}fieldset{ border:#bbb 1px solid; padding:4px;}.sqlwords{ margin:2px; border:#bbb 1px solid; width:100%;}</style><script>////構造函數function ce(e){return document.createElement(e)}/* Example:* var a = cex("DIV", {onmouseover:foo, name:'div1', id:'main'});*/function cex(e, x){ var a = ce(e); for (prop in x){  a[prop] = x[prop]; } return a;}/** function ge* Shorthand function for document.getElementById(i)*/function ge(i){return document.getElementById(i)}/** function ac* Example: ac( house, ac(roof, shingles), ac(floor, ac(tiles, grout)))*/function ac(){ if (ac.arguments.length > 1){  var a = ac.arguments[0];  for (i=1; i<ac.arguments.length; i++){   if (arguments[i])    a.appendChild(ac.arguments[i]);  }  return a; } else {  return null; }}/////ID增量function guid(){ if (!window.__id) window.__id = 0; return ++window.__id;}//======建立條件類function term(tname,fname,conn,fvalue,ttype){ this.tname=tname; this.fname=fname; this.conn=conn; this.fvalue=fvalue; this.id= guid(); this.ttype=ttype;}term.prototype.getHTML = function(){ var termFrame = cex("DIV", {  id:this.id,  className:'tmframe',  onmouseover:this.fc_term_onmouseover(),  onmouseout:this.fc_term_onmouseout()  }); //var module = cex("DIV", {  //id:'module'+this.id,  //className:'module'  //}); var tttt=this.tname+"."+this.fname; if(this.ttype!='fset')  tttt=this.tname; var mtt = cex("input", {  id:'tp'+this.id,  name:'fname'+this.id,  type:"hidden",  value:this.ttype  }); var fname = cex("DIV", {  id:'fname'+this.id,  className:'fname',  innerHTML:tttt  }); var conn = cex("DIV", {  id:'conn'+this.id,  className:'conn',  innerHTML:this.conn  }); var fvalue = cex("DIV", {  id:'fvalue'+this.id,  className:'fvalue',  innerHTML:this.fvalue  }); var handlebox = cex("div", {  id:'handlebox'+this.id,  className:"handlebox"  }); var mdel = cex("div", {  id:'tmdel'+this.id,  onclick:this.fc_mdel_onclick(),  className:"rbox",  title:"刪除此條件",  innerHTML: 'X'  }); var mup = cex("div", {  id:'tmup'+this.id,  onclick:this.fc_mup_onclick(),  className:"rbox",  title:"向上移動",  innerHTML: '↑'  }); var mdown = cex("div", {  id:'tmdown'+this.id,  onclick:this.fc_mdown_onclick(),  className:"rbox",  title:"向下移動",  innerHTML: '↓'  }); var mzkh = cex("div", {  id:'tzkh'+this.id,  onclick:this.fc_mzkh_onclick(),  className:"rbox",  title:"添加左括號",  innerHTML: '('  }); var mykh = cex("div", {  id:'tykh'+this.id,  onclick:this.fc_mykh_onclick(),  className:"rbox",  title:"添加右括號",  innerHTML: ')'  }); var mand = cex("div", {  id:'tand'+this.id,  onclick:this.fc_mand_onclick(),  className:"rbox",  title:"添加并條件",  innerHTML: 'and'  }); var mor = cex("div", {  id:'tor'+this.id,  onclick:this.fc_mor_onclick(),  className:"rbox",  title:"添加或條件",  innerHTML: 'or'  }); // Build DIV ac (termFrame,   mtt,   ac (handlebox,   mdel,   mup,   mdown,   mykh,   mzkh,   mand,   mor   ),   fname,   conn,   fvalue  ); return termFrame;}term.prototype.highlight = function(){ ge("handlebox"+this.id).className = 'handlebox_view';  ge(this.id).className = 'tmFrame_highlight';}term.prototype.lowlight = function(){ ge("handlebox"+this.id).className = 'handlebox'; ge(this.id).className = 'tmFrame';}term.prototype.remove = function(){ var _this = ge(this.id); _this.parentNode.removeChild(_this);}term.prototype.moveup = function(){ var _this = ge(this.id); var pre_this = _this.previousSibling; if(pre_this!=null){  _this.parentNode.insertBefore(_this,pre_this);  this.lowlight(); }}term.prototype.movedown = function(){ var _this = ge(this.id); var next_this = _this.nextSibling; if(next_this!=null){  _this.parentNode.insertBefore(next_this,_this);  this.lowlight(); }}term.prototype.addzkh = function(){ var _this = ge(this.id); var tzkh = new term('
主站蜘蛛池模板:
宝坻区|
阳东县|
茂名市|
汉寿县|
永寿县|
化隆|
竹溪县|
宿迁市|
金沙县|
大同县|
抚顺市|
唐山市|
上蔡县|
达日县|
远安县|
芜湖市|
霸州市|
资兴市|
临西县|
山东|
柳河县|
马龙县|
长治市|
包头市|
灵石县|
区。|
政和县|
太和县|
苍梧县|
甘德县|
武川县|
通榆县|
凌海市|
孟村|
麻栗坡县|
星子县|
洪湖市|
新邵县|
远安县|
甘南县|
无极县|