fix select bug

This commit is contained in:
sentsin 2017-02-28 12:30:26 +08:00
parent 7172a9a23e
commit d9259ace92
3 changed files with 53 additions and 24 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -77,12 +77,14 @@ layui.define('layer', function(exports){
//下拉选择框 //下拉选择框
select: function(){ select: function(){
var TIPS = '请选择', CLASS = 'layui-form-select', TITLE = 'layui-select-title' var TIPS = '请选择', CLASS = 'layui-form-select', TITLE = 'layui-select-title'
,NONE = 'layui-select-none', initValue = '' ,NONE = 'layui-select-none', initValue = '', thatInput
,selects = $(ELEM).find('select'), hide = function(e, clear){ ,selects = $(ELEM).find('select'), hide = function(e, clear){
if(!$(e.target).parent().hasClass(TITLE) || clear){ if(!$(e.target).parent().hasClass(TITLE) || clear){
$('.'+CLASS).removeClass(CLASS+'ed'); $('.'+CLASS).removeClass(CLASS+'ed');
thatInput && initValue && thatInput.val(initValue);
} }
thatInput = null;
} }
,events = function(reElem, disabled, isSearch){ ,events = function(reElem, disabled, isSearch){
@ -98,11 +100,17 @@ layui.define('layer', function(exports){
//展开下拉 //展开下拉
var showDown = function(){ var showDown = function(){
reElem.addClass(CLASS+'ed'); reElem.addClass(CLASS+'ed');
initValue = input.val();
dds.removeClass(HIDE); dds.removeClass(HIDE);
}, hideDown = function(){ }, hideDown = function(){
reElem.removeClass(CLASS+'ed'); reElem.removeClass(CLASS+'ed');
input.blur(); input.blur();
notOption(input.val(), function(none){
if(none){
initValue = dl.find('.'+THIS).html();
input && input.val(initValue);
}
});
}; };
//点击标题区域 //点击标题区域
@ -138,33 +146,50 @@ layui.define('layer', function(exports){
} }
}); });
//搜索匹配 //检测值是否不属于select项
if(isSearch){ var notOption = function(value, callback, keyup){
var search = function(e){ var num = 0;
var value = this.value, num = 0, keyCode = e.keyCode; layui.each(dds, function(){
var othis = $(this)
,text = othis.text()
,not = text.indexOf(value) === -1;
if(value === '' || (keyup ? not : value !== text)) num++;
keyup && othis[not ? 'addClass' : 'removeClass'](HIDE);
});
var none = num === dds.length;
return callback(none), none;
};
if(keyCode === 9 || keyCode === 13 || keyCode === 37 || keyCode === 38 || keyCode === 39 || keyCode === 40){ //搜索匹配
var search = function(e){
var value = this.value, keyCode = e.keyCode;
if(keyCode === 9 || keyCode === 13
|| keyCode === 37 || keyCode === 38
|| keyCode === 39 || keyCode === 40
){
return false; return false;
} }
layui.each(dds, function(){ notOption(value, function(none){
var othis = $(this), not = othis.text().indexOf(value) === -1; if(none){
if(not) num++;
othis[not ? 'addClass' : 'removeClass'](HIDE);
});
if(num === dds.length){
dl.find('.'+NONE)[0] || dl.append('<p class="'+ NONE +'">无匹配项</p>'); dl.find('.'+NONE)[0] || dl.append('<p class="'+ NONE +'">无匹配项</p>');
} else { } else {
dl.find('.'+NONE).remove(); dl.find('.'+NONE).remove();
} }
}, true);
}; };
input.on('keyup', search).on('blur', function(){ if(isSearch){
input.val(initValue); input.on('keyup', search).on('blur', function(e){
thatInput = input;
initValue = dl.find('.'+THIS).html();
if(!initValue){
input.val('');
}
}); });
} }
//选择 //选择
dds.on('click', function(){ dds.on('click', function(){
var othis = $(this), value = othis.attr('lay-value'); var othis = $(this), value = othis.attr('lay-value');
@ -179,6 +204,10 @@ layui.define('layer', function(exports){
,value: value ,value: value
,othis: reElem ,othis: reElem
}); });
hideDown();
return false;
}); });
reElem.find('dl>dt').on('click', function(e){ reElem.find('dl>dt').on('click', function(e){