如何使用自动完成(jQuery UI)更改选择事件上的文本框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8285198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:17:15  来源:igfitidea点击:

How to change textbox value on select event with autocomplete (jQuery UI)

jqueryjquery-uiautocomplete

提问by el_pup_le

Why won't #input-myBoxclear when I select an item? It seems autocomplete is preventing my .val('')to work so how can I workaround this?

为什么#input-myBox在我选择一个项目时不会清除?似乎自动完成阻止了我.val('')的工作,所以我该如何解决这个问题?

$("#input-myBox").autocomplete({
    source: response,
    minLength: 1,
    select: function(event, ui) {
                var selectedObj = ui.item;
                $("#input-myBox").appendTo(".foo");
                $("#input-myBox").val('');  
    }
});

回答by el_pup_le

event.preventDefault()stops autocomplete setting the field.

event.preventDefault()停止自动完成设置字段。

select: function (event, ui) {
  event.preventDefault();
  var selectedObj = ui.item;
  $("#input-myBox").appendTo(".foo");
  $("#input-myBox").val('');  
}

回答by kd12

Also, you can use 'return false;' to stop autocomplete setting the field.

此外,您可以使用'return false;' 停止自动完成设置字段。

select: function (event, ui) { 
    var selectedObj = ui.item;        
    $("#input-myBox").appendTo(".foo");
    $("#input-myBox").val('');  
    return false;
}

回答by HerrimanCoder

If you just want to substitute the selected value for something else:

如果您只想将选定的值替换为其他值:

select: function( event, ui ) {
  ui.item.value = substituteWord(ui.item.value);
}

回答by psynnott

Is it #input-mybox(lowercase b) or #input-myBox(uppercase b) ?

#input-mybox(小写 b) 还是#input-myBox(大写 b) ?

This might be your problem :)

这可能是你的问题:)

Edit: rob beat me to it

编辑:抢夺了我

回答by gdoron is supporting Monica

There is inconsistent with your code:

与您的代码不一致:

$("#input-mybox").appendTo(".foo");
$("#input-myBox").val('');  

"#input-myBox" || "#input-mybox" ???

“#input-myBox” || “#input-mybox”???