src/lay/modules/form.js
function(){
        var TIPS = '请选择', CLASS = 'layui-form-select', TITLE = 'layui-select-title'
        ,NONE = 'layui-select-none', initValue = '', thatInput
        ,selects = elemForm.find('select')
        
        //隐藏 select
        ,hide = function(e, clear){
          if(!$(e.target).parent().hasClass(TITLE) || clear){
            $('.'+CLASS).removeClass(CLASS+'ed ' + CLASS+'up');
            thatInput && initValue && thatInput.val(initValue);
          }
          thatInput = null;
        }
        
        //各种事件
        ,events = function(reElem, disabled, isSearch){
          var select = $(this)
          ,title = reElem.find('.' + TITLE)
          ,input = title.find('input')
          ,dl = reElem.find('dl')
          ,dds = dl.children('dd')
          ,index =  this.selectedIndex //当前选中的索引
          ,nearElem; //select 组件当前选中的附近元素，用于辅助快捷键功能
          
          if(disabled) return;
          
          //展开下拉
          var showDown = function(){
            var top = reElem.offset().top + reElem.outerHeight() + 5 - $win.scrollTop()
            ,dlHeight = dl.outerHeight();
            
            index = select[0].selectedIndex; //获取最新的 selectedIndex
            reElem.addClass(CLASS+'ed');
            dds.removeClass(HIDE);
            nearElem = null;

            //初始选中样式
            dds.eq(index).addClass(THIS).siblings().removeClass(THIS);

            //上下定位识别
            if(top + dlHeight > $win.height() && top >= dlHeight){
              reElem.addClass(CLASS + 'up');
            }
            
            followScroll();
          }
          
          //隐藏下拉
          ,hideDown = function(choose){
            reElem.removeClass(CLASS+'ed ' + CLASS+'up');
            input.blur();
            nearElem = null;
            
            if(choose) return;
            
            notOption(input.val(), function(none){
              var selectedIndex = select[0].selectedIndex;
              
              //未查询到相关值
              if(none){
                initValue = $(select[0].options[selectedIndex]).html(); //重新获得初始选中值
                
                //如果是第一项，且文本值等于 placeholder，则清空初始值
                if(selectedIndex === 0 && initValue === input.attr('placeholder')){
                  initValue = '';
                };

                //如果有选中值，则将输入框纠正为该值。否则清空输入框
                input.val(initValue || '');
              }
            });
          }
          
          //定位下拉滚动条
          ,followScroll = function(){  
            var thisDd = dl.children('dd.'+ THIS);
            
            if(!thisDd[0]) return;
            
            var posTop = thisDd.position().top
            ,dlHeight = dl.height()
            ,ddHeight = thisDd.height();
            
            //若选中元素在滚动条不可见底部
            if(posTop > dlHeight){
              dl.scrollTop(posTop + dl.scrollTop() - dlHeight + ddHeight - 5);
            }
            
            //若选择玄素在滚动条不可见顶部
            if(posTop < 0){
              dl.scrollTop(posTop + dl.scrollTop() - 5);
            }
          };
          
          //点击标题区域
          title.on('click', function(e){
            reElem.hasClass(CLASS+'ed') ? (
              hideDown()
            ) : (
              hide(e, true), 
              showDown()
            );
            dl.find('.'+NONE).remove();
          }); 
          
          //点击箭头获取焦点
          title.find('.layui-edge').on('click', function(){
            input.focus();
          });
          
          //select 中 input 键盘事件
          input.on('keyup', function(e){ //键盘松开
            var keyCode = e.keyCode;
            
            //Tab键展开
            if(keyCode === 9){
              showDown();
            }
          }).on('keydown', function(e){ //键盘按下
            var keyCode = e.keyCode;

            //Tab键隐藏
            if(keyCode === 9){
              hideDown();
            }
            
            //标注 dd 的选中状态
            var setThisDd = function(prevNext, thisElem1){
              var nearDd, cacheNearElem
              e.preventDefault();

              //得到当前队列元素  
              var thisElem = function(){
                var thisDd = dl.children('dd.'+ THIS);
                
                //如果是搜索状态，且按 Down 键，且当前可视 dd 元素在选中元素之前，
                //则将当前可视 dd 元素的上一个元素作为虚拟的当前选中元素，以保证递归不中断
                if(dl.children('dd.'+  HIDE)[0] && prevNext === 'next'){
                  var showDd = dl.children('dd:not(.'+ HIDE +',.'+ DISABLED +')')
                  ,firstIndex = showDd.eq(0).index();
                  if(firstIndex >=0 && firstIndex < thisDd.index() && !showDd.hasClass(THIS)){
                    return showDd.eq(0).prev()[0] ? showDd.eq(0).prev() : dl.children(':last');
                  }
                }

                if(thisElem1 && thisElem1[0]){
                  return thisElem1;
                }
                if(nearElem && nearElem[0]){
                  return nearElem;
                }
       
                return thisDd;
                //return dds.eq(index);
              }();
              
              cacheNearElem = thisElem[prevNext](); //当前元素的附近元素
              nearDd =  thisElem[prevNext]('dd:not(.'+ HIDE +')'); //当前可视元素的 dd 元素

              //如果附近的元素不存在，则停止执行，并清空 nearElem
              if(!cacheNearElem[0]) return nearElem = null;
              
              //记录附近的元素，让其成为下一个当前元素
              nearElem = thisElem[prevNext]();

              //如果附近不是 dd ，或者附近的 dd 元素是禁用状态，则进入递归查找
              if((!nearDd[0] || nearDd.hasClass(DISABLED)) && nearElem[0]){
                return setThisDd(prevNext, nearElem);
              }
              
              nearDd.addClass(THIS).siblings().removeClass(THIS); //标注样式
              followScroll(); //定位滚动条
            };
            
            if(keyCode === 38) setThisDd('prev'); //Up 键
            if(keyCode === 40) setThisDd('next'); //Down 键
            
            //Enter 键
            if(keyCode === 13){ 
              e.preventDefault();
              dl.children('dd.'+THIS).trigger('click');
            }
          });
          
          //检测值是否不属于 select 项
          var notOption = function(value, callback, origin){
            var num = 0;
            layui.each(dds, function(){
              var othis = $(this)
              ,text = othis.text()
              ,not = text.indexOf(value) === -1;
              if(value === '' || (origin === 'blur') ? value !== text : not) num++;
              origin === 'keyup' && othis[not ? 'addClass' : 'removeClass'](HIDE);
            });
            var none = num === dds.length;
            return callback(none), none;
          };
          
          //搜索匹配
          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;
            }
            
            notOption(value, function(none){
              if(none){
                dl.find('.'+NONE)[0] || dl.append('<p class="'+ NONE +'">无匹配项</p>');
              } else {
                dl.find('.'+NONE).remove();
              }
            }, 'keyup');
            
            if(value === ''){
              dl.find('.'+NONE).remove();
            }
            
            followScroll(); //定位滚动条
          };
          
          if(isSearch){
            input.on('keyup', search).on('blur', function(e){
              var selectedIndex = select[0].selectedIndex;
              
              thatInput = input; //当前的 select 中的 input 元素
              initValue = $(select[0].options[selectedIndex]).html(); //重新获得初始选中值
              
              //如果是第一项，且文本值等于 placeholder，则清空初始值
              if(selectedIndex === 0 && initValue === input.attr('placeholder')){
                initValue = '';
              };
              
              setTimeout(function(){
                notOption(input.val(), function(none){
                  initValue || input.val(''); //none && !initValue
                }, 'blur');
              }, 200);
            });
          }

          //选择
          dds.on('click', function(){
            var othis = $(this), value = othis.attr('lay-value');
            var filter = select.attr('lay-filter'); //获取过滤器
            
            if(othis.hasClass(DISABLED)) return false;
            
            if(othis.hasClass('layui-select-tips')){
              input.val('');
            } else {
              input.val(othis.text());
              othis.addClass(THIS);
            }

            othis.siblings().removeClass(THIS);
            select.val(value).removeClass('layui-form-danger')
            layui.event.call(this, MOD_NAME, 'select('+ filter +')', {
              elem: select[0]
              ,value: value
              ,othis: reElem
            });

            hideDown(true);
            return false;
          });
          
          reElem.find('dl>dt').on('click', function(e){
            return false;
          });
          
          $(document).off('click', hide).on('click', hide); //点击其它元素关闭 select
        }
        
        selects.each(function(index, select){
          var othis = $(this)
          ,hasRender = othis.next('.'+CLASS)
          ,disabled = this.disabled
          ,value = select.value
          ,selected = $(select.options[select.selectedIndex]) //获取当前选中项
          ,optionsFirst = select.options[0];
          
          if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
          
          var isSearch = typeof othis.attr('lay-search') === 'string'
          ,placeholder = optionsFirst ? (
            optionsFirst.value ? TIPS : (optionsFirst.innerHTML || TIPS)
          ) : TIPS;

          //替代元素
          var reElem = $(['<div class="'+ (isSearch ? '' : 'layui-unselect ') + CLASS 
          ,(disabled ? ' layui-select-disabled' : '') +'">'
            ,'<div class="'+ TITLE +'">'
              ,('<input type="text" placeholder="'+ placeholder +'" '
                +('value="'+ (value ? selected.html() : '') +'"') //默认值
                +(isSearch ? '' : ' readonly') //是否开启搜索
                +' class="layui-input'
                +(isSearch ? '' : ' layui-unselect') 
              + (disabled ? (' ' + DISABLED) : '') +'">') //禁用状态
            ,'<i class="layui-edge"></i></div>'
            ,'<dl class="layui-anim layui-anim-upbit'+ (othis.find('optgroup')[0] ? ' layui-select-group' : '') +'">'
            ,function(options){
              var arr = [];
              layui.each(options, function(index, item){
                if(index === 0 && !item.value){
                  arr.push('<dd lay-value="" class="layui-select-tips">'+ (item.innerHTML || TIPS) +'</dd>');
                } else if(item.tagName.toLowerCase() === 'optgroup'){
                  arr.push('<dt>'+ item.label +'</dt>'); 
                } else {
                  arr.push('<dd lay-value="'+ item.value +'" class="'+ (value === item.value ?  THIS : '') + (item.disabled ? (' '+DISABLED) : '') +'">'+ item.innerHTML +'</dd>');
                }
              });
              arr.length === 0 && arr.push('<dd lay-value="" class="'+ DISABLED +'">没有选项</dd>');
              return arr.join('');
            }(othis.find('*')) +'</dl>'
          ,'</div>'].join(''));
          
          hasRender[0] && hasRender.remove(); //如果已经渲染，则Rerender
          othis.after(reElem);          
          events.call(this, reElem, disabled, isSearch);
        });
      }
function(){
        var TIPS = '请选择', CLASS = 'layui-form-select', TITLE = 'layui-select-title'
        ,NONE = 'layui-select-none', initValue = '', thatInput
        ,selects = elemForm.find('select')
        
        //隐藏 select
        ,hide = function(e, clear){
          if(!$(e.target).parent().hasClass(TITLE) || clear){
            $('.'+CLASS).removeClass(CLASS+'ed ' + CLASS+'up');
            thatInput && initValue && thatInput.val(initValue);
          }
          thatInput = null;
        }
        
        //各种事件
        ,events = function(reElem, disabled, isSearch){
          var select = $(this)
          ,title = reElem.find('.' + TITLE)
          ,input = title.find('input')
          ,dl = reElem.find('dl')
          ,dds = dl.children('dd')
          ,index =  this.selectedIndex //当前选中的索引
          ,nearElem; //select 组件当前选中的附近元素，用于辅助快捷键功能
          
          if(disabled) return;
          
          //展开下拉
          var showDown = function(){
            var top = reElem.offset().top + reElem.outerHeight() + 5 - $win.scrollTop()
            ,dlHeight = dl.outerHeight();
            
            index = select[0].selectedIndex; //获取最新的 selectedIndex
            reElem.addClass(CLASS+'ed');
            dds.removeClass(HIDE);
            nearElem = null;

            //初始选中样式
            dds.eq(index).addClass(THIS).siblings().removeClass(THIS);

            //上下定位识别
            if(top + dlHeight > $win.height() && top >= dlHeight){
              reElem.addClass(CLASS + 'up');
            }
            
            followScroll();
          }
          
          //隐藏下拉
          ,hideDown = function(choose){
            reElem.removeClass(CLASS+'ed ' + CLASS+'up');
            input.blur();
            nearElem = null;
            
            if(choose) return;
            
            notOption(input.val(), function(none){
              var selectedIndex = select[0].selectedIndex;
              
              //未查询到相关值
              if(none){
                initValue = $(select[0].options[selectedIndex]).html(); //重新获得初始选中值
                
                //如果是第一项，且文本值等于 placeholder，则清空初始值
                if(selectedIndex === 0 && initValue === input.attr('placeholder')){
                  initValue = '';
                };

                //如果有选中值，则将输入框纠正为该值。否则清空输入框
                input.val(initValue || '');
              }
            });
          }
          
          //定位下拉滚动条
          ,followScroll = function(){  
            var thisDd = dl.children('dd.'+ THIS);
            
            if(!thisDd[0]) return;
            
            var posTop = thisDd.position().top
            ,dlHeight = dl.height()
            ,ddHeight = thisDd.height();
            
            //若选中元素在滚动条不可见底部
            if(posTop > dlHeight){
              dl.scrollTop(posTop + dl.scrollTop() - dlHeight + ddHeight - 5);
            }
            
            //若选择玄素在滚动条不可见顶部
            if(posTop < 0){
              dl.scrollTop(posTop + dl.scrollTop() - 5);
            }
          };
          
          //点击标题区域
          title.on('click', function(e){
            reElem.hasClass(CLASS+'ed') ? (
              hideDown()
            ) : (
              hide(e, true), 
              showDown()
            );
            dl.find('.'+NONE).remove();
          }); 
          
          //点击箭头获取焦点
          title.find('.layui-edge').on('click', function(){
            input.focus();
          });
          
          //select 中 input 键盘事件
          input.on('keyup', function(e){ //键盘松开
            var keyCode = e.keyCode;
            
            //Tab键展开
            if(keyCode === 9){
              showDown();
            }
          }).on('keydown', function(e){ //键盘按下
            var keyCode = e.keyCode;

            //Tab键隐藏
            if(keyCode === 9){
              hideDown();
            }
            
            //标注 dd 的选中状态
            var setThisDd = function(prevNext, thisElem1){
              var nearDd, cacheNearElem
              e.preventDefault();

              //得到当前队列元素  
              var thisElem = function(){
                var thisDd = dl.children('dd.'+ THIS);
                
                //如果是搜索状态，且按 Down 键，且当前可视 dd 元素在选中元素之前，
                //则将当前可视 dd 元素的上一个元素作为虚拟的当前选中元素，以保证递归不中断
                if(dl.children('dd.'+  HIDE)[0] && prevNext === 'next'){
                  var showDd = dl.children('dd:not(.'+ HIDE +',.'+ DISABLED +')')
                  ,firstIndex = showDd.eq(0).index();
                  if(firstIndex >=0 && firstIndex < thisDd.index() && !showDd.hasClass(THIS)){
                    return showDd.eq(0).prev()[0] ? showDd.eq(0).prev() : dl.children(':last');
                  }
                }

                if(thisElem1 && thisElem1[0]){
                  return thisElem1;
                }
                if(nearElem && nearElem[0]){
                  return nearElem;
                }
       
                return thisDd;
                //return dds.eq(index);
              }();
              
              cacheNearElem = thisElem[prevNext](); //当前元素的附近元素
              nearDd =  thisElem[prevNext]('dd:not(.'+ HIDE +')'); //当前可视元素的 dd 元素

              //如果附近的元素不存在，则停止执行，并清空 nearElem
              if(!cacheNearElem[0]) return nearElem = null;
              
              //记录附近的元素，让其成为下一个当前元素
              nearElem = thisElem[prevNext]();

              //如果附近不是 dd ，或者附近的 dd 元素是禁用状态，则进入递归查找
              if((!nearDd[0] || nearDd.hasClass(DISABLED)) && nearElem[0]){
                return setThisDd(prevNext, nearElem);
              }
              
              nearDd.addClass(THIS).siblings().removeClass(THIS); //标注样式
              followScroll(); //定位滚动条
            };
            
            if(keyCode === 38) setThisDd('prev'); //Up 键
            if(keyCode === 40) setThisDd('next'); //Down 键
            
            //Enter 键
            if(keyCode === 13){ 
              e.preventDefault();
              dl.children('dd.'+THIS).trigger('click');
            }
          });
          
          //检测值是否不属于 select 项
          var notOption = function(value, callback, origin){
            var num = 0;
            layui.each(dds, function(){
              var othis = $(this)
              ,text = othis.text()
              ,not = text.indexOf(value) === -1;
              if(value === '' || (origin === 'blur') ? value !== text : not) num++;
              origin === 'keyup' && othis[not ? 'addClass' : 'removeClass'](HIDE);
            });
            var none = num === dds.length;
            return callback(none), none;
          };
          
          //搜索匹配
          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;
            }
            
            notOption(value, function(none){
              if(none){
                dl.find('.'+NONE)[0] || dl.append('<p class="'+ NONE +'">无匹配项</p>');
              } else {
                dl.find('.'+NONE).remove();
              }
            }, 'keyup');
            
            if(value === ''){
              dl.find('.'+NONE).remove();
            }
            
            followScroll(); //定位滚动条
          };
          
          if(isSearch){
            input.on('keyup', search).on('blur', function(e){
              var selectedIndex = select[0].selectedIndex;
              
              thatInput = input; //当前的 select 中的 input 元素
              initValue = $(select[0].options[selectedIndex]).html(); //重新获得初始选中值
              
              //如果是第一项，且文本值等于 placeholder，则清空初始值
              if(selectedIndex === 0 && initValue === input.attr('placeholder')){
                initValue = '';
              };
              
              setTimeout(function(){
                notOption(input.val(), function(none){
                  initValue || input.val(''); //none && !initValue
                }, 'blur');
              }, 200);
            });
          }

          //选择
          dds.on('click', function(){
            var othis = $(this), value = othis.attr('lay-value');
            var filter = select.attr('lay-filter'); //获取过滤器
            
            if(othis.hasClass(DISABLED)) return false;
            
            if(othis.hasClass('layui-select-tips')){
              input.val('');
            } else {
              input.val(othis.text());
              othis.addClass(THIS);
            }

            othis.siblings().removeClass(THIS);
            select.val(value).removeClass('layui-form-danger')
            layui.event.call(this, MOD_NAME, 'select('+ filter +')', {
              elem: select[0]
              ,value: value
              ,othis: reElem
            });

            hideDown(true);
            return false;
          });
          
          reElem.find('dl>dt').on('click', function(e){
            return false;
          });
          
          $(document).off('click', hide).on('click', hide); //点击其它元素关闭 select
        }
        
        selects.each(function(index, select){
          var othis = $(this)
          ,hasRender = othis.next('.'+CLASS)
          ,disabled = this.disabled
          ,value = select.value
          ,selected = $(select.options[select.selectedIndex]) //获取当前选中项
          ,optionsFirst = select.options[0];
          
          if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
          
          var isSearch = typeof othis.attr('lay-search') === 'string'
          ,placeholder = optionsFirst ? (
            optionsFirst.value ? TIPS : (optionsFirst.innerHTML || TIPS)
          ) : TIPS;

          //替代元素
          var reElem = $(['<div class="'+ (isSearch ? '' : 'layui-unselect ') + CLASS 
          ,(disabled ? ' layui-select-disabled' : '') +'">'
            ,'<div class="'+ TITLE +'">'
              ,('<input type="text" placeholder="'+ placeholder +'" '
                +('value="'+ (value ? selected.html() : '') +'"') //默认值
                +(isSearch ? '' : ' readonly') //是否开启搜索
                +' class="layui-input'
                +(isSearch ? '' : ' layui-unselect') 
              + (disabled ? (' ' + DISABLED) : '') +'">') //禁用状态
            ,'<i class="layui-edge"></i></div>'
            ,'<dl class="layui-anim layui-anim-upbit'+ (othis.find('optgroup')[0] ? ' layui-select-group' : '') +'">'
            ,function(options){
              var arr = [];
              layui.each(options, function(index, item){
                if(index === 0 && !item.value){
                  arr.push('<dd lay-value="" class="layui-select-tips">'+ (item.innerHTML || TIPS) +'</dd>');
                } else if(item.tagName.toLowerCase() === 'optgroup'){
                  arr.push('<dt>'+ item.label +'</dt>'); 
                } else {
                  arr.push('<dd lay-value="'+ item.value +'" class="'+ (value === item.value ?  THIS : '') + (item.disabled ? (' '+DISABLED) : '') +'">'+ item.innerHTML +'</dd>');
                }
              });
              arr.length === 0 && arr.push('<dd lay-value="" class="'+ DISABLED +'">没有选项</dd>');
              return arr.join('');
            }(othis.find('*')) +'</dl>'
          ,'</div>'].join(''));
          
          hasRender[0] && hasRender.remove(); //如果已经渲染，则Rerender
          othis.after(reElem);          
          events.call(this, reElem, disabled, isSearch);
        });
      }
function ( ) { var TIPS = '请选择', CLAS S = 'la u -form-select', TITL E = 'la u -select-title' , NONE = 'layui-select-none' , initValue = '' , thatInput , selects = elemForm . find ( 'select' ) //隐藏 select , hide = function ( e , clear ) { if ( ! $ ( e . target ) . parent ( ) . hasClass ( TITLE ) || clear ) { $ ( '.' + CLASS ) . removeClass ( CLASS + 'ed ' + CLASS + 'up' ) ; thatInput && initValue && thatInput . val ( initValue ) ; } thatInput = null ; } //各种事件 , events = function ( reElem , disabled , isSearch ) { var select = $ ( this ) , title = reElem . find ( '.' + TITLE ) , input = title . find ( 'input' ) , dl = reElem . find ( 'dl' ) , dds = dl . children ( 'dd' ) , index = this . selectedIndex //当前选中的索引 , nearElem ; //select 组件当前选中的附近元素，用于辅助快捷键功能 if ( disabled ) return ; //展开下拉 var showDown = function ( ) { var top = reElem . offset ( ) . top + reElem . outerHeight ( ) + 5 - $win . scrollTop ( ) , dlHeight = dl . outerHeight ( ) ; index = select [ 0 ] . selectedIndex ; //获取最新的 selectedIndex reElem . addClass ( CLASS + 'ed' ) ; dds . removeClass ( HIDE ) ; nearElem = null ; //初始选中样式 dds . eq ( index ) . addClass ( THIS ) . siblings ( ) . removeClass ( THIS ) ; //上下定位识别 if ( top + dlHeight > $win . height ( ) && top >= dlHeight ) { reElem . addClass ( CLASS + 'up' ) ; } followScroll ( ) ; } //隐藏下拉 , hideDown = function ( choose ) { reElem . removeClass ( CLASS + 'ed ' + CLASS + 'up' ) ; input . blur ( ) ; nearElem = null ; if ( choose ) return ; notOption ( input . val ( ) , function ( none ) { var selectedIndex = select [ 0 ] . selectedIndex ; //未查询到相关值 if ( none ) { initValue = $ ( select [ 0 ] . options [ selectedIndex ] ) . html ( ) ; //重新获得初始选中值 //如果是第一项，且文本值等于 placeholder，则清空初始值 if ( selectedIndex === 0 && initValue === input . attr ( 'placeholder' ) ) { initValue = '' ; } ; //如果有选中值，则将输入框纠正为该值。否则清空输入框 input . val ( initValue || '' ) ; } } ) ; } //定位下拉滚动条 , followScroll = function ( ) { var thisDd = dl . children ( 'dd.' + THIS ) ; if ( ! thisDd [ 0 ] ) return ; var posTop = thisDd . position ( ) . top , dlHeight = dl . height ( ) , ddHeight = thisDd . height ( ) ; //若选中元素在滚动条不可见底部 if ( posTop > dlHeight ) { dl . scrollTop ( posTop + dl . scrollTop ( ) - dlHeight + ddHeight - 5 ) ; } //若选择玄素在滚动条不可见顶部 if ( posTop < 0 ) { dl . scrollTop ( posTop + dl . scrollTop ( ) - 5 ) ; } } ; //点击标题区域 title . on ( 'click' , function ( e ) { reElem . hasClass ( CLASS + 'ed' ) ? ( hideDown ( ) ) : ( hide ( e , true ) , showDown ( ) ) ; dl . find ( '.' + NONE ) . remove ( ) ; } ) ; //点击箭头获取焦点 title . find ( '.layui-edge' ) . on ( 'click' , function ( ) { input . focus ( ) ; } ) ; //select 中 input 键盘事件 input . on ( 'keyup' , function ( e ) { //键盘松开 var keyCode = e . keyCode ; //Tab键展开 if ( keyCode === 9 ) { showDown ( ) ; } } ) . on ( 'keydown' , function ( e ) { //键盘按下 var keyCode = e . keyCode ; //Tab键隐藏 if ( keyCode === 9 ) { hideDown ( ) ; } //标注 dd 的选中状态 var setThisDd = function ( prevNext , thisElem1 ) { var nearDd , cacheNearElem e . preventDefault ( ) ; //得到当前队列元素   var thisElem = function ( ) { var thisDd = dl . children ( 'dd.' + THIS ) ; //如果是搜索状态，且按 Down 键，且当前可视 dd 元素在选中元素之前， //则将当前可视 dd 元素的上一个元素作为虚拟的当前选中元素，以保证递归不中断 if ( dl . children ( 'dd.' + HIDE ) [ 0 ] && prevNext === 'next' ) { var showDd = dl . children ( 'dd:not(.' + HIDE + ',.' + DISABLED + ')' ) , firstIndex = showDd . eq ( 0 ) . index ( ) ; if ( firstIndex >= 0 && firstIndex < thisDd . index ( ) && ! showDd . hasClass ( THIS ) ) { return showDd . eq ( 0 ) . prev ( ) [ 0 ] ? showDd . eq ( 0 ) . prev ( ) : dl . children ( ':last' ) ; } } if ( thisElem1 && thisElem1 [ 0 ] ) { return thisElem1 ; } if ( nearElem && nearElem [ 0 ] ) { return nearElem ; } return thisDd ; //return dds.eq(index); } ( ) ; cacheNearElem = thisElem [ prevNext ] ( ) ; //当前元素的附近元素 nearDd = thisElem [ prevNext ] ( 'dd:not(.' + HIDE + ')' ) ; //当前可视元素的 dd 元素 //如果附近的元素不存在，则停止执行，并清空 nearElem if ( ! cacheNearElem [ 0 ] ) return nearElem = null ; //记录附近的元素，让其成为下一个当前元素 nearElem = thisElem [ prevNext ] ( ) ; //如果附近不是 dd ，或者附近的 dd 元素是禁用状态，则进入递归查找 if ( ( ! nearDd [ 0 ] || nearDd . hasClass ( DISABLED ) ) && nearElem [ 0 ] ) { return setThisDd ( prevNext , nearElem ) ; } nearDd . addClass ( THIS ) . siblings ( ) . removeClass ( THIS ) ; //标注样式 followScroll ( ) ; //定位滚动条 } ; if ( keyCode === 38 ) setThisDd ( 'prev' ) ; //Up 键 if ( keyCode === 40 ) setThisDd ( 'next' ) ; //Down 键 //Enter 键 if ( keyCode === 13 ) { e . preventDefault ( ) ; dl . children ( 'dd.' + THIS ) . trigger ( 'click' ) ; } } ) ; //检测值是否不属于 select 项 var notOption = function ( value , callback , origin ) { var num = 0 ; layui . each ( dds , function ( ) { var othis = $ ( this ) , text = othis . text ( ) , not = text . indexOf ( value ) === - 1 ; if ( value === '' || ( origin === 'blur' ) ? value !== text : not ) num ++ ; origin === 'keyup' && othis [ not ? 'addClass' : 'removeClass' ] ( HIDE ) ; } ) ; var none = num === dds . length ; return callback ( none ) , none ; } ; //搜索匹配 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 ; } notOption ( value , function ( none ) { if ( none ) { dl . find ( '.' + NONE ) [ 0 ] || dl . append ( '<p class="' + NONE + '">无匹配项</p>'); } else { dl . find ( '.' + NONE ) . remove ( ) ; } } , 'keyup' ) ; if ( value === '' ) { dl . find ( '.' + NONE ) . remove ( ) ; } followScroll ( ) ; //定位滚动条 } ; if ( isSearch ) { input . on ( 'keyup' , search ) . on ( 'blur' , function ( e ) { var selectedIndex = select [ 0 ] . selectedIndex ; thatInput = input ; //当前的 select 中的 input 元素 initValue = $ ( select [ 0 ] . options [ selectedIndex ] ) . html ( ) ; //重新获得初始选中值 //如果是第一项，且文本值等于 placeholder，则清空初始值 if ( selectedIndex === 0 && initValue === input . attr ( 'placeholder' ) ) { initValue = '' ; } ; setTimeout ( function ( ) { notOption ( input . val ( ) , function ( none ) { initValue || input . val ( '' ) ; //none && !initValue } , 'blur' ) ; } , 200 ) ; } ) ; } //选择 dds . on ( 'click' , function ( ) { var othis = $ ( this ) , value = othis . attr ( 'lay-value' ) ; var filter = select . attr ( 'lay-filter' ) ; //获取过滤器 if ( othis . hasClass ( DISABLED ) ) return false ; if ( othis . hasClass ( 'layui-select-tips' ) ) { input . val ( '' ) ; } else { input . val ( othis . text ( ) ) ; othis . addClass ( THIS ) ; } othis . siblings ( ) . removeClass ( THIS ) ; select . val ( value ) . removeClass ( 'layui-form-danger' ) layui . event . call ( this , MOD_NAME , 'select(' + filter + ')' , { elem : select [ 0 ] , value : value , othis : reElem } ) ; hideDown ( true ) ; return false ; } ) ; reElem . find ( 'dl>dt' ) . on ( 'click' , function ( e ) { return false ; } ) ; $ ( document ) . off ( 'click' , hide ) . on ( 'click' , hide ) ; //点击其它元素关闭 select } selects . each ( function ( index , select ) { var othis = $ ( this ) , hasRender = othis . next ( '.' + CLASS ) , disabled = this . disabled , value = select . value , selected = $ ( select . options [ select . selectedIndex ] ) //获取当前选中项 , optionsFirst = select . options [ 0 ] ; if ( typeof othis . attr ( 'lay-ignore' ) === 'string' ) return othis . show ( ) ; var isSearch = typeof othis . attr ( 'lay-search' ) === 'string' , placeholder = optionsFirst ? ( optionsFirst . value ? TIPS : ( optionsFirst . innerHTML || TIPS ) ) : TIPS ; //替代元素 var reElem = $ ( [ '<div class="' + ( isSearch ? '' : 'layui-unselect ' ) + CLASS , ( disabled ? ' layui-select-disabled' : '' ) + '">' , '<div class="' + TITLE + '">' , ( '<input type="text" placeholder="' + placeholder + '" ' + ( 'value="' + ( value ? selected . html ( ) : '' ) + '"' ) //默认值 + ( isSearch ? '' : ' readonly' ) //是否开启搜索 + ' class="layui-input' + ( isSearch ? '' : ' layui-unselect' ) + ( disabled ? ( ' ' + DISABLED ) : '' ) + '">' ) //禁用状态 , '<i class="layui-edge"></i></div>' , '<dl class="layui-anim layui-anim-upbit' + ( othis . find ( 'optgroup' ) [ 0 ] ? ' layui-select-group' : '' ) + '">' , function ( options ) { var arr = [ ] ; layui . each ( options , function ( index , item ) { if ( index === 0 && ! item . value ) { arr . push ( '<dd lay-value="" class="layui-select-tips">' + ( item . innerHTML || TIPS ) + '</dd>' ) ; } else if ( item . tagName . toLowerCase ( ) === 'optgroup' ) { arr . push ( '<dt>' + item . label + '</dt>' ) ; } else { arr . push ( '<dd lay-value="' + item . value + '" class="' + ( value === item . value ? THIS : '' ) + ( item . disabled ? ( ' ' + DISABLED ) : '' ) + '">' + item . innerHTML + '</dd>' ) ; } } ) ; arr . length === 0 && arr . push ( '<dd lay-value="" class="' + DISABLED + '">没有选项</dd>'); return arr . join ( '' ) ; } ( othis . find ( '*' ) ) + '</dl>' , '</div>' ] . join ( '' ) ) ; hasRender [ 0 ] && hasRender . remove ( ) ; //如果已经渲染，则Rerender othis . after ( reElem ) ; events . call ( this , reElem , disabled , isSearch ) ; } ) ; }
下拉选择框
下拉选择框