简体   繁体   English

使jquery自动完成不使用所选项的标签填充输入值

[英]Make jquery autocomplete not to fill the input value with the label of selected item

Is there any way to make jquery autocomplete plugin only respond to the "select" callback, not to fill the target input with the label value of the selected item after user selection? 有没有办法让jquery自动完成插件只响应“select”回调,而不是用户选择后用所选项目的标签值填充目标输入?

I am trying but still no success, waiting for help... 我正在努力,但仍然没有成功,等待帮助......

Regards. 问候。 Larry 拉里

I know that this is an old question but, for people that doesn't want to use the setTimeout function, the solution is to change the property ui.item.value to "" 我知道这是一个老问题但是,对于不想使用setTimeout函数的人来说,解决方案是将属性ui.item.value更改为“”

Code : 代码

$('#ac').autocomplete({
    source : ["hello", "how", "do", "you", "do"],
    select: function(event, ui){
        if (ui.item && ui.item.value){
            ui.item.value="";
        } 
    }
})

Try code below ;) 请尝试以下代码;)

$('#ac').autocomplete({
    source : ["hello", "how", "do", "you", "do"],
    close: function(event, ui){
          $('#ac').val('');
    }
});

If your source array is an array of objects with "label" and "value" properties, the plugin handles this for you. 如果源数组是具有“label”和“value”属性的对象数组,则插件会为您处理此问题。

See jQuery api for autocomplete : 请参阅jQuery api以获取自动完成功能

 $('#autocomplete').autocomplete({ source: [ { label: 'This is a descriptive label' , value: 'Just the value' } ,{ label: '2 is better than one' , value: '2'} ,{ label: 'third times the charm' , value:'Three' } ] }) 
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script> <input type='text' id='autocomplete'> 

SO's code snippet doesn't like it but it works SO的代码片段不喜欢它,但它有效

Here is an even simpler way (using experimentX's code): 这是一种更简单的方法(使用experimentX的代码):

$('#ac').autocomplete({
    source : ["hello", "how", "do", "you", "do"],
    select: function(event, ui){
        $('#ac').css('color','white');    
    }
})

DEMO ON JS FIDDLE 关于JS FIDDLE的演示

$('#ac').autocomplete({
    source : ["hello", "how", "do", "you", "do"],
    select: function(event, ui){
        setTimeout(function (){
          $('#ac').val('');    
        }, 1000)
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM