简体   繁体   English

jQuery自动建议

[英]Jquery autosuggest

I am new to jquery. 我是jquery新手。 I am using the below code for showing autosuggest in text box. 我正在使用下面的代码在文本框中显示自动提示。

$().ready(function() {
    function formatItem(row) {
        return row[0] + " (<strong>id: " + row[1] + "</strong>)";
    }

    function formatResult(row) {
        return row[0].replace(/(<.+?>)/gi, '');
    }

    //$("#suggest1").autocomplete(cities);  

    $("#custName").autocomplete(arrNames, {
        multiple: false,
        minChars: 0,
        width: 190,
        matchContains: true,
        autoFill: false,
        mustMatch: true,
        max: 20,                
    }
});
});

My problem is I want to call a javascript function along with the index of arrNames as parameter when user select one name from autosuggest. 我的问题是,当用户从autosuggest中选择一个名称时,我想与arrNames的索引arrNames作为参数调用JavaScript函数。 Please help me. 请帮我。

Here is how you do it (using jQuery ui autocomplete) : 这是您的操作方法(使用jQuery ui autocomplete):

$("#custName").autocomplete(
        source: arrNames,
        select: function (event, ui) {
           //Do stuff here
        }     
    }

From jqueryUI website : 从jqueryUI网站:

Select 选择

Triggered when an item is selected from the menu; 从菜单中选择一个项目时触发; ui.item refers to the selected item. ui.item引用选定的项目。 The default action of select is to replace the text field's value with the value of the selected item. select的默认操作是将文本字段的值替换为所选项目的值。 Canceling this event prevents the value from being updated, but does not prevent the menu from closing. 取消此事件将阻止更新值,但不会阻止菜单关闭。

EDIT : 编辑:

It seems you are using Autocomplete plugin from http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ 看来您正在使用http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/中的 Autocomplete插件

This plugin is deprecated... You should use jQuery ui autocomplete : http://jqueryui.com/demos/autocomplete/ 不建议使用此插件...您应该使用jQuery ui autocomplete: http : //jqueryui.com/demos/autocomplete/

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

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