1

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

示例不起作用:

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/

4

1 回答 1

4

阻止 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/

于 2012-06-21T18:26:12.447 回答