我正在使用jQuery开发一个自动完成建议表单,我的问题是KEYPRESS发送一个不完整查询的POST请求(其余-1字符用于实际查询)。
这是jQuery代码:
$(function(){
search = $(".left");
search_input = $(".left input.text");
search_input.click(function(){
$(".left form").append("<div class=\"search-capa admitido\"></div>");
search_capa = $(".search-capa");
result_search_capa = $(".search-capa");
})
$(".left value.text").keypress(function(){
value = $(".left input.text").val();
value = value.replace(" ","+");
console.log(value);
getConec(value);
})
function getConec(kery){
$.post("suggest.php",{q:kery},function(data){
result_search_capa.html(data);
var li = $('.search-capa a');
var liSelected;
moveoption()
})
}
function moveoption(){
$(document).keydown(function(e){
if(e.which === 40){
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.next();
if(next.length > 0){
liSelected = next.addClass('selected');
$('.left input.text').val(liSelected.text());
}else{
liSelected = li.eq(0).addClass('selected');
$('.left input.text').val(liSelected.text());
}
}else{
liSelected = li.eq(0).addClass('selected');
$('.left input.text').val(liSelected.text());
}
}else if(e.which === 38){
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.prev();
if(next.length > 0){
liSelected = next.addClass('selected');
$('.left input.text').val(liSelected.text());
}else{
liSelected = li.last().addClass('selected');
$('.left input.text').val(liSelected.text());
}
}else{
liSelected = li.last().addClass('selected');
$('.left input.text').val(liSelected.text());
}
}
});
}
})
如果我搜索“Web开发人员”,POST请求会发送“Web develope”(最后一个字符总是丢失)。为什么?有什么建议吗?