jQuery建议从JSON数组搜索? [英] Jquery suggested search from JSON Array?
问题描述
我有一个JSON数组
json_encode($results);
和一个搜索框
<form method="post" action="" name="search_form">
<span class="search-text">
<input class="box" name="search" type="text" id="search_input" />
</form>
我想做的是在用户在搜索框中输入内容时搜索JSON数组并提出建议. 数组的一部分是:
What I'd like to do is search the JSON Array as the user types in the search box and suggest entries. Part of the array is:
[{"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}
我知道问题在于自动完成功能需要标签和值.当我从数据库中提取阵列时,如何使AutoComplete认为每个项目的标签都由Manufacturer + Model组成?
I know the issue is that AutoComplete is requiring a Label and a Value. As my array is pulled from a database, how do I go about making AutoComplete think that the label for each item consists of Manufacturer + Model?
推荐答案
如果需要为$.autocomplete
的source
提供制造商和型号的数组,请使用:
If you need to provide an array of the Manufacturer and Model to $.autocomplete
's source
, use:
$(document).ready(function(){
var devices = <?= json_encode($results); ?>;
var results = [];
$.each(devices, function(k,v){
results.push(v.Manufacturer + " " + v.Model);
});
$("#search_input").autocomplete({source: results});
});
就是这样.很抱歉,我以前的复杂性是,我基本上是为您制作自己的自动完成功能,却没有意识到您正在尝试使用内置于jQuery UI中的自动完成功能.我已经用[jquery-ui]
这篇关于jQuery建议从JSON数组搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!