标签:
/* input_select_start */
var dropDownInputObj;
function viewMyDropDowns(){
$(‘.inputSelect‘).on(‘focus‘,function(ev){
var X = $(this).offset().top;
var Y = $(this).offset().left;
var W = $(this).width();
$(‘.mydropdown-menu1‘).css({"top":X-25,"left":Y-3,"width":W+80});
dropDownInputObj=$(this);
if (!isEmpty($(this).val())) {
$(‘.mydropdown-menu1‘).removeClass(‘hide‘);
}else{
$(‘.mydropdown-menu1‘).addClass(‘hide‘);
}
var filter=$(this).val();
var rqur=$(this).attr(‘url‘)+"filter="+filter;
bindDropDownData(rqur,filter);
});
$(‘.inputSelect‘).on(‘input‘,function(ev){
var filter=$(this).val();
var rqur=$(this).attr(‘url‘)+"filter="+filter;
bindDropDownData(rqur,filter);
dropDownInputObj=$(this);
});
}
function bindDropDownData(rqur,filter){
$(‘.mydropdown-menu1‘).empty();
//!isEmpty (filter) && filter.trim().length>0 &&
if ( !isEmpty (rqur) ) {
$(‘.mydropdown-menu1‘).removeClass(‘hide‘);
$.ajax({
type : "POST",
url : rqur,
cache: false,
data : filter,
timeout: 59001,
dataType: "json",
success : function(data) {
hstModalDialogHide();
if( data.ret==1){
var html="";
for (var i = 0; i < data.obj.length; i++) {
html+=" <li code=\""+data.obj[i].code+"\">"+data.obj[i].name+"</li>";
}
$(‘.mydropdown-menu1‘).empty().append(html);
initDropDownLis();
$(‘.mydropdown-menu1‘).find(‘li‘).eq(0).css("background-color", "#ccc");
$(‘.mydropdown-menu1‘).find(‘li‘).eq(0).addClass(‘selectedz‘);
}else{
alert(data.message);
}
},
error : function() {
hstModalDialogHide();
alert("用户操作处理失败,请稍后再试。");
}
});
return;
}
}
function initDropDownLis(){
$(‘.mydropdown-menu1 li‘).on(‘click‘,function(){
var code=$(this).attr(‘code‘);
var value=$(this).html();
$(dropDownInputObj).val(value);
$(dropDownInputObj).parent().find(‘input‘).each(function (){
if ($(this).hasClass(‘need‘)) {
$(this).val(code);
}
});
$(‘.mydropdown-menu1‘).addClass(‘hide‘);
});
$(‘.mydropdown-menu1 li‘).hover(function(){
$(this).css("background-color","#ccc");
$(this).addClass(‘selectedz‘);
},function(){
$(this).css("background-color","white");
$(this).removeClass(‘selectedz‘);
});
}
var li_index_selected=0;//当前通过键盘上下键选中选项的角标
$(document).keydown(function(event){
if (!$(‘.mydropdown-menu1‘).hasClass(‘hide‘)) {
if(event.keyCode == 38){ //↑
if (li_index_selected>=0 ) {
$(‘.mydropdown-menu1‘).find(‘li‘).css("background-color", "white");
$(‘.mydropdown-menu1‘).find(‘li‘).removeClass(‘selectedz‘);
$(‘.mydropdown-menu1‘).find(‘li‘).eq(li_index_selected).css("background-color", "#ccc");
$(‘.mydropdown-menu1‘).find(‘li‘).eq(li_index_selected).addClass(‘selectedz‘);
li_index_selected--;
}
}else if (event.keyCode == 40){ //↓
if (li_index_selected<$(‘.mydropdown-menu1‘).find(‘li‘).length) {
$(‘.mydropdown-menu1‘).find(‘li‘).css("background-color", "white");
$(‘.mydropdown-menu1‘).find(‘li‘).removeClass(‘selectedz‘);
$(‘.mydropdown-menu1‘).find(‘li‘).eq(li_index_selected).css("background-color", "#ccc");
$(‘.mydropdown-menu1‘).find(‘li‘).eq(li_index_selected).addClass(‘selectedz‘);
$(document).scrollTop($(‘.mydropdown-menu1‘).find(‘li‘).eq(li_index_selected));
li_index_selected++;
}
}else if (event.keyCode == 13){ //Enter
var code= $(‘.mydropdown-menu1‘).find(‘.selectedz‘).attr(‘code‘);
var value= $(‘.mydropdown-menu1‘).find(‘.selectedz‘).html();
$(dropDownInputObj).val(value);
$(dropDownInputObj).parent().find(‘input‘).each(function (){
if ($(this).hasClass(‘need‘)) {
$(this).val(code);
}
});
$(‘.mydropdown-menu1‘).addClass(‘hide‘);
event.preventDefault();
}
if (li_index_selected<0) {
li_index_selected=0;
}
if (li_index_selected>$(‘.mydropdown-menu1‘).find(‘li‘).length) {
li_index_selected=$(‘.mydropdown-menu1‘).find(‘li‘).length;
}
try {
var container = $(‘.mydropdown-menu1‘),
scrollTo = $(‘.mydropdown-menu1‘).find(‘li‘).eq(li_index_selected);
container.scrollTop(
scrollTo.offset().top - container.offset().top + container.scrollTop()-$(‘.mydropdown-menu1‘).find(‘li‘).eq(li_index_selected).height()*2
);
} catch (e) {
console.info(e);
}
}
});
$(document).click(function(e){
if (!$(dropDownInputObj).is(":focus")) {
$(‘.mydropdown-menu1‘).addClass(‘hide‘);
}
});
/* input_select_end */
标签:
原文地址:http://www.cnblogs.com/zrpblog/p/5385431.html