select( event, ui )
的 jQuery 自动完成文档选项说:
Triggered when an item is selected from the menu; ui.item refers to the selected item. The default action of select is to replace the text field's value with the value of the selected item.
Cancelling this event prevents the value from being updated, but does not prevent the menu from closing.
那么如何取消事件呢?这是我的代码。
$("#txt1").autocomplete({
minLength: 1,
source: "abc.php",
select: function(event, ui)
{
event.preventDefault();
//alert("Select");
var label= ui.item.label;
var value= ui.item.value;
$('#txt1').val(ui.item.label);
}
});
最佳答案
我知道这是一个非常古老的问题,但我自己也遇到了这个问题。对我来说,答案是将 event.preventDefault();
放在 select 方法的末尾,如下所示:
$("#txt1").autocomplete({
minLength: 1,
source: "abc.php",
select: function(event, ui)
{
//alert("Select");
var label= ui.item.label;
var value= ui.item.value;
$('#txt1').val(ui.item.label);
event.preventDefault(); // <---- I moved!
}
});
对我来说,如果将 event.preventDefault();
放在函数的开头,则默认事件不会触发,函数的其余部分也不会执行。如果它移到最后,那么它就会按预期工作。希望这对其他人有帮助。
关于jquery - 如何防止自动完成jquery中的默认选择事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3400997/