使用箭头键聚焦时,使用Jquery自动完成功能在输入上显示自定义标签

时间:2012-06-21 18:23:12

标签: jquery jquery-ui autocomplete jquery-ui-autocomplete

当悬停/聚焦于项目结果时,如何覆盖输入上显示项目标签的默认行为?

示例不起作用:

JavaScript的:

$('#ac').autocomplete({
    source : ["hello", "how", "do", "you", "do"],
    focus: function(event, ui){
         console.log("print")
         $(this).val('my custom label to show in input');    
    }
})

HTML:

<input type="text" id="ac"/>

http://jsfiddle.net/34fSg/14/

1 个答案:

答案 0 :(得分:4)

Prevent the default action of the event,用输入的值替换为焦点项的值:

$('#ac').autocomplete({
    source : ["hello", "how", "do", "you", "do"],
    focus: function(event, ui){
         this.value = 'my custom label to show in input';
         event.preventDefault(); // <-----
    }
})

示例: http://jsfiddle.net/4jJyb/