简体   繁体   English

来自UL LI的JQuery自动完成(作为源代码)

[英]JQuery Autocomplete from UL LI (as Source)

This is my ul li : 这是我的意思:

<ul id="red" class="treeview-red">
        <li><span>Item 1</span>
            <ul>
                <li><span>Item 1.0</span>
                    <ul>
                        <li><span>Item 1.0.0</span></li>
                    </ul></li>
                <li><span>Item 1.1</span></li>
                <li><span>Item 1.2</span>
                    <ul>
                        <li><span>Item 1.2.0</span>
                            <ul>
                                <li><span>Item 1.2.0.0</span></li>
                                <li><span>Item 1.2.0.1</span></li>
                                <li><span>Item 1.2.0.2</span></li>
                            </ul></li>
...............
.................. 
</ul>

I want to have a autocomplete textfield from the items present in here. 我想从这里的项目中获得一个自动完成的文本字段。

Can anybody help? 有人可以帮忙吗?

What you want to do is iterate over each span(based on your Markup), grab the text of each, and push into an array the key:value pairs of label and value . 你想要做的是遍历每个跨度(基于你的标记),获取每个跨度的文本,并将key:value labelvalue key:value对推入数组。

var sources = [];
$('span').each(function(i,ele){
    sources.push({'label': $(ele).text(), 'value' : $(ele).text()});
});

Then when you're done building it, (after the $.each()), we'll simply use the new array as our source for the input. 然后,当你完成构建它(在$ .each()之后)时,我们将简单地使用新数组作为输入源。

$('input').autocomplete({
    source: sources
});

You can see a quick and dirty working example here 你可以在这里看到一个快速而肮脏的工作示例

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

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