jquery - 如何防止自动完成jquery中的默认选择事件

标签 jquery jquery-ui-autocomplete

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/

相关文章:

javascript - Openlayers 和 Jquery 自动完成从矢量图层打开弹出窗口

django - 使用 url 作为源的 jQueryUI 自动完成(我使用 Django)

jquery-ui - Jquery UI 自动完成匹配输入的第一个字母

jquery - 在 Jquery-ui-autocomplete 中设置 VALUE 的样式

javascript - 仅播放一次基于 jQuery 的动画介绍

javascript - Data.filter 在 IE 8 中不起作用?

c# - 如何在 asp.net 中获取客户端当前日期和时间?

javascript - jQuery 可以给我一个按类名排除某些子标签的子级列表吗?

javascript - 使用来自 asp WebMethod 的数据填充 jquery 自动完成

基于选择选项值的 JQuery 自动完成