selectArray

var selectArray = new Array();

/**
* ÈÃFlexÏÂÀ­¿òÑ¡ÖÐijһֵ
*/
function setFlexValue(selectId,value) {
if(getFlexSelect(selectId)) {
getFlexSelect(selectId).setValue(value);
} else {
alert("ûÓÐÕÒµ½Ñ¡ÔñÏî");
}
}
/**
* È¡FlexÏÂÀ­¿òÖµ
*/
function getFlexValue(selectId) {
if(getFlexSelect(selectId)) {
return getFlexSelect(selectId).getValue();
} else {
alert("ûÓÐÕÒµ½Ñ¡ÔñÏî");
}
}
/**
* È¡FlexÏÂÀ­¿òÎı¾
*/
function getFlexText(selectId) {
if(getFlexSelect(selectId)) {
return getFlexSelect(selectId).getText();
} else {
alert("ûÓÐÕÒµ½Ñ¡ÔñÏî");
}
}
/**
* FlexÏÂÀ­¿òÖµÔö¼ÓÒ»ÅúÑ¡Ïî
*/
function addFlexOptions(selectId,objArray) {
if(this.selectArray!=undefined&&objArray!=undefined) {
for(var j=0;j<objArray.length;j++) {
addOption(selectId,objArray[j].name,objArray[j].value);
}
}
}
/**
* Çå¿ÕFlexÏÂÀ­¿òËùÓÐÑ¡Ïî
*/
function emptyFlex(selectId) {
if(getFlexSelect(selectId)) {
getFlexSelect(selectId).removeOption();
} else {
alert("ûÓÐÕÒµ½Ñ¡ÔñÏî");
}
}
/**
* Çå¿ÕFlexÏÂÀ­¿òËùÓÐÑ¡Ïî²¢±£ÁôÒ»¸öĬÈÏoption
*/
function emptyFlexSetDefVal(selectId, def) {
if(getFlexSelect(selectId)) {
getFlexSelect(selectId).removeOption();
addFlexOption(selectId,def['name'],def['value']) ;
} else {
alert("ûÓÐÕÒµ½Ñ¡ÔñÏî");
}
}
/**
* FlexÏÂÀ­¿òÖµÔö¼ÓÒ»¸öÑ¡Ïî
*/
function addFlexOption(selectId,name,value) {
if(getFlexSelect(selectId)) {
getFlexSelect(selectId).addOption(name,value);
} else {
alert("ûÓÐÕÒµ½Ñ¡ÔñÏî");
}
}
/**
* FlexÏÂÀ­¿ò½ûÓã¨Ö»½ûÓÃInput£¬Hidden±£Áô£©
*/
function setFlexDisabled(selectId) {
if(getFlexSelect(selectId)) {
getFlexSelect(selectId).disabled();
} else {
alert("ûÓÐÕÒµ½Ñ¡ÔñÏî");
}
}

function getFlexSelect(selectId) {

if(this.selectArray!=undefined) {
for(var i=0;i<selectArray.length;i++) {
if(selectArray[i].id==selectId) {
return selectArray[i].selectObject;
}
}
}
return undefined;
}
(function($) {
$.flexselect = function(select, options) { this.init(select, options); };

$.extend($.flexselect.prototype, {
settings: {
allowMismatch: false,
selectedClass: "flexselect_selected",
dropdownClass: "flexselect_dropdown",
inputClass:"flexselect_input",
inputIdTransform: function(id) { return id + "_flexselect"; },
inputNameTransform: function(name) { return; },
dropdownIdTransform: function(id) { return id + "_flexselect_dropdown"; }
},
select: null,
input: null,
hidden: null,
dropdown: null,
dropdownList: null,
cache: [],
results: [],
lastAbbreviation: null,
abbreviationBeforeFocus: null,
selectedIndex: 0,
picked: false,
dropdownMouseover: false, // Workaround for poor IE behaviors
inputMouseover: false,
myevent : null,
init: function(select, options) {
$.extend(this.settings, options);
this.select = $(select);
this.preloadCache();
this.renderControls();
this.wire();
},

preloadCache: function() {
this.cache = this.select.children("option").map(function() {
return { name: $.trim($(this).text()), value: $(this).val(), score: 0.0 };
});
},
renderControls: function() {
var selected = this.select.children("option:selected");

this.hidden = $("<input type='hidden'/>").attr({
id: this.select.attr("id"),
name: this.select.attr("name")
});
this.hidden.val(selected.val());
this.input = $("<input type='text' autocomplete='off' />").attr({
id: this.settings.inputIdTransform(this.select.attr("id")),
name: this.settings.inputNameTransform(this.select.attr("name")),
accesskey: this.select.attr("accesskey"),
tabindex: this.select.attr("tabindex"),
style: this.select.attr("style")
}).addClass(this.settings.inputClass)
.bind('onchange1',this.select.attr("onchange")||function(){}).
val($.trim(selected.text()));

this.myevent = $("<iframe style='z-index:-1;background-color: #E6E4E7; ' scrolling='no' frameborder='0'></iframe>");
this.dropdown = $("<div style='height:150px;width:100% ;overflow-x:hidden; ' scrolling='no'></div>").attr({
id: this.settings.dropdownIdTransform(this.select.attr("id"))
}).addClass(this.settings.dropdownClass);
this.dropdown.append(this.myevent);
this.dropdownList = $("<ul></ul>");
this.dropdown.append(this.dropdownList);
// this.input.after($('<img class="button" src="dropdown.gif" />'));
this.select.after(this.input).after(this.hidden).remove();
$("body").append(this.dropdown);

},
wire: function() {
var self = this;

this.input.click(function() {
self.lastAbbreviation = null;
self.focus();
});

this.input.mouseup(function(event) {
event.preventDefault();
});

this.input.focus(function() {
self.abbreviationBeforeFocus = self.input.val();
self.input.select();
//if (!self.picked)
self.filterResults(true);
});

this.input.blur(function() {
if (!self.dropdownMouseover) {
self.hide();
if (!self.picked) self.reset();
}
});
this.dropdownList.mouseover(function (event) {
if (event.target.tagName == "LI") {
var rows = self.dropdown.find("li");
self.markSelected(rows.index($(event.target)));
}
});
this.dropdownList.mouseleave(function () {
self.markSelected(-1);
});
this.dropdownList.mouseup(function (event) {
self.pickSelected();
self.focusAndHide();
});
this.dropdown.mouseover(function (event) {
self.dropdownMouseover = true;
});
this.dropdown.mouseleave(function (event) {
self.dropdownMouseover = false;
self.focusAndHide();
});
this.input.mouseover(function (event) {
self.inputMouseover = true;
});
this.input.mouseleave(function (event) {
self.inputMouseover = false;
});
this.dropdown.mousedown(function (event) {
event.preventDefault();
});

this.input.keyup(function(event) {
switch (event.keyCode) {
case 13: // return
event.preventDefault();
self.pickSelected();
self.focusAndHide();
break;
case 27: // esc
event.preventDefault();
self.reset();
self.focusAndHide();
break;
default:
self.filterResults();
break;
}
});

this.input.keydown(function(event) {
switch (event.keyCode) {
case 9: // tab
self.pickSelected();
self.hide();
break;
case 33: // pgup
event.preventDefault();
self.markFirst();
break;
case 34: // pgedown
event.preventDefault();
self.markLast();
break;
case 38: // up
event.preventDefault();
self.moveSelected(-1);
break;
case 40: // down
event.preventDefault();
self.moveSelected(1);
break;
case 13: // return
case 27: // esc
event.preventDefault();
event.stopPropagation();
break;
}
});
},

filterResults: function(focs) {
var abbreviation = this.input.val();
if (abbreviation == this.lastAbbreviation){return;}

var results = [];
$.each(this.cache, function() {
this.score = LiquidMetal.score(this.name, abbreviation);
if(focs) {
if (this.score >= 0.0) results.push(this);
} else{
if (this.score > 0.0) results.push(this);
}

});
this.results = results;

this.sortResults();
this.renderDropdown();
this.markFirst();
this.lastAbbreviation = abbreviation;
this.picked = false;
},
sortResults: function() {
this.results.sort(function(a, b) { return b.score - a.score; });
},

renderDropdown: function() {
var dropdownBorderWidth = this.dropdown.outerWidth() - this.dropdown.innerWidth();
var inputOffset = this.input.offset();
this.dropdown.css({
width: (this.input.outerWidth() - dropdownBorderWidth) + "px",
top: (inputOffset.top + this.input.outerHeight()) + "px",
left: inputOffset.left + "px",
Height:"150px",
overflow:'auto'
})
var list = this.dropdownList.html("");
$.each(this.results, function() {
// list.append($("<li/>").html(this.name + " <small>[" + Math.round(this.score*100)/100 + "]</small>"));
list.append($("<li/>").html(this.name));
});
this.dropdown.show();
var frameHeight = (this.input.outerHeight()*(this.results.length));
frameHeight = frameHeight<150?'150':frameHeight;
this.myevent.css({
position: 'absolute',
width: (this.input.outerWidth() - dropdownBorderWidth-1) + "px",
top:"0px",
left: "0px",
height:frameHeight+'px'
})
},

markSelected: function(n) {
if (n > this.results.length) return;

var rows = this.dropdown.find("li");
rows.removeClass(this.settings.selectedClass);
this.selectedIndex = n;

if (n >= 0) $(rows[n]).addClass(this.settings.selectedClass);
},

pickSelected: function() {

var selected = this.results[this.selectedIndex];
var oldValue=this.hidden.val();
if (selected) {
this.input.val(selected.name);
this.hidden.val(selected.value);
this.picked = true;
if(selected.value!=oldValue)
this.input.trigger("onchange1") ;
} else if (this.settings.allowMismatch) {
this.hidden.val("");
} else {
this.reset();
}
},
addOption: function(a,b) {
var obj = new Object();
obj.name=a;
obj.value=b;
obj.score="0.0";
this.cache.push(obj);
//µÚÒ»¸öÔªËØ×Ô¶¯Ñ¡ÖÐ
if(this.cache.length==1) {
this.setValue(b);
}
},
removeOption: function() {
this.input.val("");
this.hidden.val("");
this.cache = [];
},
hide: function() {
this.dropdown.hide();
this.lastAbbreviation = null;
},
setValue: function(value){
var hide = this.hidden;
var thisin = this.input;
$.each(this.cache, function() {
if(this.value==value){
hide.val(value);
thisin.val(this.name);
}
});
},
getValue: function(){
return this.hidden.val();
},
getText: function(){
return this.input.val();
},
disabled: function() {
this.input.attr("disabled",true);
},
moveSelected: function(n) { this.markSelected(this.selectedIndex+n); },
markFirst: function() { this.markSelected(0); },
markLast: function() { this.markSelected(this.results.length - 1); },
reset: function() { this.input.val(this.abbreviationBeforeFocus); },
focus: function() { this.input.focus(); },
focusAndHide: function() { this.focus; this.hide(); }
});

$.fn.flexselect = function(options) {
this.each(function() {
if (this.tagName == "SELECT"){
var temp = new $.flexselect(this, options);
var obj = new Object();
obj.id=this.id;
obj.selectObject = temp;
selectArray.push(obj);
}
});
return this;
};
})(jQuery);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值