本文實例講述了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('
主站蜘蛛池模板:
运城市|
兴宁市|
新绛县|
夏河县|
和平县|
云梦县|
峨边|
高邑县|
南和县|
柳河县|
抚宁县|
赫章县|
蓬莱市|
祁门县|
巴里|
金沙县|
嘉定区|
梁河县|
高青县|
紫阳县|
绍兴市|
北海市|
津南区|
奉化市|
永州市|
晋宁县|
本溪|
新郑市|
泰安市|
大足县|
祁东县|
乐至县|
永顺县|
盘山县|
定州市|
汝阳县|
始兴县|
南充市|
广宗县|
中牟县|
绵竹市|