/** * 自定义下拉列 * * Licensed under the GPL: * http://www.gnu.org/licenses/gpl.txt * * Copyright 2010 goldensoft.com * */ var gSelect = {}; gSelect.bindInputs = new Map(); gSelect.currentOpen = null; gSelect.canClose = true; gSelect.isDrpWdInit=false; gSelect.init = function (D){ if (gSelect.isDrpWdInit) return; var domain = ""; if (typeof(HOST_DOMAIN)!='undefined'){ if (HOST_DOMAIN!=""){ domain = ""; } } try{ D.open(); D.write(""); D.write(""); D.write(domain); // Write Domain D.write(""); D.write(""); D.write('
'); D.write(""); // 判断IE浏览器,IE浏览器close会触发load,死循环 if(navigator.userAgent.indexOf("MSIE")>0) { gSelect.isDrpWdInit = true; } D.close(); } catch(E) { } gSelect.iframe = $("#__ieselect"); gSelect.iframediv = gSelect.iframe.contents().find("#iframediv"); } /** * 绑定下拉事件到控件 * parm: * id -- 需要绑定的input控件ID * url -- 取值URL(必须返回规定的格式) * params -- 调用HTTP参数 * codeid -- code回写的input控件ID(可选) * showclosebt -- 是否显示关闭按钮(可选,默认true,若为false,则关闭及分页不再显示) */ gSelect.bind = function (id,_url,_params,_codeid,_showclosebt){ if (this.bindInputs.containsKey(id)){ var f = this.bindInputs.get(id); f.params = _params; if (_showclosebt==undefined) _showclosebt = true; f.isShowCloseBt = _showclosebt; return; } if (!this.outerDiv){ this.iframe = null; this.iframediv=null; this.outerDiv = $("#_smanDisp"); this.outerDiv.mouseenter(function (e){ gSelect.canClose = false; }); this.outerDiv.mouseleave(function (e){ gSelect.canClose = true; }); var src = "about:blank"; if (typeof(HOST_DOMAIN)!='undefined'){ if (HOST_DOMAIN!=""){ src = "javascript:void((function(){document.open();document.domain='"+HOST_DOMAIN+"';document.write('');document.close()})())" } } var _iframe = document.createElement("iframe"); _iframe.src = src; _iframe.scrolling = "no"; _iframe.id = "__ieselect"; _iframe.setAttribute('frameborder', '0' , 0); var smanDisp = document.getElementById("_smanDisp"); if (_iframe.attachEvent){ _iframe.attachEvent("onload", function(){ //判断对象是否为空,同时捕获异常 bug:52994 edit by gt 2013-6-28 try{ if("undefined"!=typeof(_iframe.contentWindow) && "undefined" != typeof(_iframe.contentWindow.document)){ var D = _iframe.contentWindow.document; gSelect.init(D); } }catch(e){ } }); }else{ _iframe.onload = function(){ var D = _iframe.contentWindow.document; gSelect.init(D); } } smanDisp.appendChild(_iframe); } this.bindInputs.put(id,new bindInput(id,_url,_params,_codeid,_showclosebt)); function bindInput(id,_url,_params,_codeid,_showclosebt){ this.id = "#"+id; this.input = $(this.id); this.params = _params; this.url = _url; if (_codeid) this.codeid = _codeid; if (_showclosebt==undefined){ _showclosebt = true; } this._showclosebt = _showclosebt; this.input.bind("click",function (e){ // this.id -> input.id if (null!=gSelect.currentOpen&&gSelect.currentOpen == this.id)return; if (this.readOnly){ this.value=""; } var fun = gSelect.bindInputs.get(this.id); onFocus(this,fun); }); this.input.bind("blur",function (e){ try{ if (!gSelect.canClose) return; }catch(e){ } gSelect.closeDivPage(); }); this.input.bind("change",function (e){ var focusedElement = document.activeElement; // 获取当前焦点所在的元素 console.log(focusedElement); this.value = this.value.toUpperCase(); gSelect.nextpage(this.id,1); }); function onFocus(ipt,e){ ipt.select(); gSelect.load(ipt.id,e,1); ipt.focus(); } this.input.keyup(function (e){ //this.value = this.value.toUpperCase(); //gSelect.nextpage(this.id,1); }); } } gSelect.nextpage = function (id,page){ var fun = gSelect.bindInputs.get(id); gSelect.load(id,fun,page); } gSelect.load = function(id,fun,page){ var input = $("#"+id); var param = fun.params; var strInput = input.val(); if (strInput == $("#"+id).attr("alt")) { strInput =""; } var divWidth = 50; // 增加分页 param.page = page; param.keyworld = strInput; // 打开下拉 divPosition(); $.post(fun.url,fun.params,function (result){ var allpage = result[0].allPage; var pageno = result[0].pageNo; var allcount = result[0].allCount; var closeHTML = " 关闭 "; if (!fun._showclosebt) closeHTML = ""; // 不显示关闭按钮 gSelect.iframediv.html("
"+closeHTML+"
"); var ret = result[1]; for (intTmp = 0; intTmp < ret.length; intTmp++) { addOption(ret[intTmp].value, strInput.toUpperCase(), ret[intTmp].code); } // 加入分页 if (fun._showclosebt == true) addSplitPage(parseInt(pageno),parseInt(allpage),parseInt(allcount)); // 加载完成后,让input拿到焦点 input.focus(); },"json"); gSelect.currentOpen = id; // 当前打开的对象 function addOption(value, keyw, ids) { var v = value.replace(keyw, "" + keyw + ""); var innerHTML = "
" + v + "
"; gSelect.iframediv.append(innerHTML); changeSize(); } function addSplitPage(pageno,pages,allcount){ var pagetext = ""; pagetext = '
'; if(parseInt(pageno)>1){ pagetext +=""; }else{ pagetext += " "; } pagetext += ""; pagetext += " 共"+allcount+"条 "; pagetext += ""; if(parseInt(pageno)"; } pagetext += "
"; var innerHTML = "
"+pagetext+"
"; gSelect.iframediv.append(innerHTML); changeSize(); } function divPosition() { var itop = input.offset().top + input.height() + 4; var ileft = input.offset().left; gSelect.iframediv.html("
"); gSelect.outerDiv.css("top",itop); gSelect.outerDiv.css("left",ileft); gSelect.outerDiv.css("display",""); gSelect.outerDiv.css("width",input.width()); divWidth = input.width() - 8; gSelect.iframe.width(input.width()-2); changeSize(); } function changeSize() { gSelect.iframe.height(gSelect.iframediv.height()+2); gSelect.outerDiv.css("height",gSelect.iframe.height()); } } //关闭下拉框 gSelect.closeDivPage = function closeDivPage() { gSelect.outerDiv.css("display","none"); gSelect.currentOpen = null; } gSelect.selectVal = function(div,vcode){ if (null==gSelect.currentOpen)return; var input = $("#"+gSelect.currentOpen); var value = ""; if (2==checkWebBrowse()){ // FF value = div.textContent; }else{ // IE or Other value = div.innerText; } input.val(gScript.trim(value)); // code id var f = gSelect.bindInputs.get(gSelect.currentOpen); if (f.codeid){ var codipt = $("#"+f.codeid).val(vcode); } // Fire input.trigger('change'); gSelect.closeDivPage(); } //清楚绑定的所有ID gSelect.selectClear = function(){ gSelect.bindInputs.clear(); } //移出指定ID的绑定事件 gSelect.selectRemove = function(id){ gSelect.bindInputs.remove(id); }