我创建了一个ajax下拉列表,当我按下向下箭头和向上箭头键时,我需要更改下拉值的背景颜色。当我按下关键背景更改并且它立即消失时。这不在ajax下拉列表,如果我在设置类selectedhash之前放置并发出警报,它正在工作,否则它无法正常工作。
这里的div将由带有列表的ajax结果更新。
请帮我解决这个问题。
<div class='textautocomplete'>
</div>
$(document).on("keydown", function(e) {
if (e.keyCode == 40)
{
if(chosen === "")
{
chosen = 0;
} else if((chosen+1) < $('.textautocomplete ul').length)
{
chosen++;
}
$('.textautocomplete ul').removeClass('selectedhash');
$('.textautocomplete ul:eq('+chosen+')').addClass('selectedhash');
return false;
}
if (e.keyCode == 38) {
if(chosen === "") {
chosen = 0;
} else if(chosen > 0) {
chosen--;
}
$('.textautocomplete ul').removeClass('selectedhash');
$('.textautocomplete ul:eq('+chosen+')').addClass('selectedhash');
return false;
}
});
$(".textinput").live("keyup",function(e)
{
$.post('/users/getusers',{data:dataString},function(result){
if(result!=='')
{
$('.textautocomplete').show();
$('.textautocomplete').html(result);
}
else
{
$('.textautocomplete').hide();
$('.textautocomplete').html('');
}
});
return false
});
答案 0 :(得分:0)
首先改变:&#34; .live&#34; to:&#34; .on&#34;在线:
$(".textinput").live("keyup",function(e)
,因为:
"As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()."
其次在以下位置添加keyCode过滤器代码:
$(".textinput")
例如:
if ( e . keyCode == 38 || e . keyCode == 40 ) { return false; }
工作示例小提琴:JSFiddle