简体   繁体   English

获取列表项的名称在jquery中的自动完成中单击

[英]get name of list item click in autocomplete in jquery

Hi all, 大家好,

In my application I am using autocomplete, I have list as 在我的应用程序中,我使用自动完成,我列表为

        <p>
            <input type="text" id="searchField" placeholder="Categories">
            <ul id="suggestions" data-role="listview" data-inset="true"></ul>
        </p>

I have one array name myArray and using autocomplete as: 我有一个数组名称myArray并使用自动完成:

$("#searchField").autocomplete(
                        {
                            target: $('#suggestions'),
                            source: myArray ,
                            link: 'target.html?term=',
                            minLength:0
                        });

Now I want to get the list item name on which I click and use that variable in target.html file. 现在我想获取我点击的列表项名称,并在target.html文件中使用该变量。 How to get that? 怎么做到的? Thanks in advance. 提前致谢。

From their help docs. 来自他们的帮助文档。

Callback 打回来

When using the optional callback function autoComplete will only execute code found within the callback. 使用可选的回调函数时,autoComplete只会执行回调中的代码。 The click event object is passed into the callback function for use in accessing the information contained in the selection. click事件对象被传递到回调函数,用于访问选择中包含的信息。 Here's one use case: 这是一个用例:

$("#searchField").autocomplete("update", {
    source: [ "foo", "bar", "baz" ],
    minLength: 3,
    callback: function(e) {
        var $a = $(e.currentTarget); // access the selected item
        $('#searchField').val($a.text()); // place the value of the selection into the search box
        $("#searchField").autocomplete('clear'); // clear the listview
    }
});

OPTION 1 This section will allow you to access the text field 选项1本部分允许您访问文本字段

$('#searchField').val($a.text()); // or $a.value()

so do something like this inside the callback event 所以在回调事件中做这样的事情

window.location.replace("http://stackoverflow.com?target=" + $a.text());

OPTION 2 It seems like they expect the result set to be in this format (text & value), so if you'd need other values, you'd need to resort to the jquery autocomplete (which this component is based on) 选项2看起来他们希望结果集采用这种格式(文本和值),所以如果你需要其他值,你需要求助于jquery自动完成(这个组件所基于的)

 $('#some_id').autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: 'some_url',
                        dataType: "json",
                        data: {
                            filter: request.term
                        },
                        success: function (data) {
                            response($.map(eval(data), function (item) {
                                return {
                                    label: item.Text,
                                    value: item.Value,
                                    extra_value: item.Extra_Value
                                }
                            }));
                        }
                    })
                },
                maxLength: 2,
                select: function (event, ui) {
                    $("#Some_id2").attr('value', ui.item.extra_value);
                }
            });

UPDATE aka OPTION 3 From their demo code, if you just want the text value, and don't need the ID (like in your case), just change your source format. 更新aka选项3从他们的演示代码中,如果您只需要文本值,并且不需要ID(就像您的情况一样),只需更改源格式即可。 Rather than returning a JSON result from the server return an array of strings, or convert the JSON result to a string array, which ever flavor you like 而不是从服务器返回JSON结果返回一个字符串数组,或将JSON结果转换为一个字符串数组,这就像你喜欢的那样

(code from the working sample on their demo page) (他们的演示页面上的工作示例代码)

var availableTags = ['24', 'about me',... , 'XUIJS'];

    $("#searchField").autocomplete({
        target: $('#suggestions'),
        source: availableTags,
        link: 'target.html?term=',
        minLength: 1,
        matchFromStart: false
    });

Use Callback . 使用回调。

$("#searchField").autocomplete(
                        {
                            target: $('#suggestions'),
                            source: myArray ,
                            link: 'javascript:void();',
                            minLength:0,
                            callback: function(e){

                                var name = $(e.target).attr('name');
           //This function will be called when one of the suggestions is clicked according to documentation
                                window.location = 'target.html?term='  // This line might need some tweaking. 
                            }
                        });

The code is not tested, you might need to debug this step by step. 代码未经过测试,您可能需要逐步调试此代码。

If I use 如果我使用

$("#searchField").autocomplete(
                        {
                            icon: 'arrow-l', 
                            target: $('#suggestions'),
                            source: stockArray,
                            link: 'target.html?term=',
                            minLength:0,
                            callback: function(e)
                            {

                                var nameq = $(e.currentTarget); 
                                console.log("^^^^^^^^^^^^^^^^^^^^^"+nameq);
                                //This function will be called when one of the suggestions is clicked according to documentation
                                window.location = 'target.html?term='  
                            }

                        });

I get value of nameq as 我得到nameq的价值

^^^^^^^^^^^^^^^^^^^^^[object Object] at file:///android_asset/www/index.html:115

and If I use 如果我使用

$("#searchField").autocomplete(
                        {
                            icon: 'arrow-l', 
                            target: $('#suggestions'),
                            source: stockArray,
                            link: 'target.html?term=',
                            minLength:0,
                            callback: function(e){

                             var nameq = $(e.target).attr('name');

                             console.log("^^^^^^^^^^^^^^^^^^^^^^^^^^"+nameq);
                            //This function will be called when one of the suggestions is clicked according to documentation
                            window.location = 'target.html?term='  // This line might need some tweaking. 
                        }

I get value of nameq as: 我得到nameq的价值:

^^^^^^^^^^^^^^^^^^^^^^^^^^undefined at file:///android_asset/www/index.html:115
$("#searchField").autocomplete(
                        {
                            icon: 'arrow-l', 
                            target: $('#suggestions'),
                            source: stockArray,
                            link: 'target.html?term=',
                            minLength:0,
                            callback: function(e)
                            {

                                var $a = $(e.currentTarget); // access the selected item
                                console.log("###################!!###"+$a.text());
                                $('#searchField').val($a.text()); // place the value of the selection into the search box
                                $("#searchField").autocomplete('clear'); // clear the listview
                            }


                        });

Now using $a.text() I get selected item value. 现在使用$ a.text()我得到选定的项目值。

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

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