data 参数可以使用 url 来加载元素。从文档:
data: array / string
JSON Data source used to populate the combo box. 3 options are available here:
No Data Source (default)
When left null, the combo box will not suggest anything. It can still enable the user to enter multiple entries if allowFreeEntries is set to true (default).
Static Source
You can pass an array of JSON objects, an array of strings or even a single CSV string as the data source.
For ex. data: [{id:0,name:"Paris"}, {id: 1, name: "New York"}]
Url
You can pass the url from which the component will fetch its JSON data.
Data will be fetched using a POST ajax request that will include the entered text as 'query' parameter. The results fetched from the server can be:
- an array of JSON objects (ex: [{id:...,name:...},{...}])
- a string containing an array of JSON objects ready to be parsed (ex: "[{id:...,name:...},{...}]")
- a JSON object whose data will be contained in the results property (ex: {results: [{id:...,name:...},{...}]
默认情况下,它将执行 POST 查询,但您可以使用方法参数更改它。此外,默认情况下,每次您按下某个键时,它都会使用用户输入的内容作为请求的“查询”参数触发查询。
所以...首先这里是你如何从服务器加载数据:
$(document).ready(function() {
$('#ms3').magicSuggest({
data: 'http://yoururl/data.php'
});
然后在 data.php 例如:
<?php
$data = array(array("id"=> 1, "name"=> "New York", "country"=> "United States"),
array("id"=> 2, "name"=> "Los Angeles", "country"=> "United States"),
array("id"=> 3, "name"=> "Chicago", "country"=> "United States"),
array("id"=> 4, "name"=> "Houston", "country"=> "United States"),
array("id"=> 5, "name"=> "Philadelphia", "country"=> "United States"),
array("id"=> 6, "name"=> "Paris", "country"=> "France"),
array("id"=> 7, "name"=> "Marseille", "country"=> "France"),
array("id"=> 8, "name"=> "Toulouse", "country"=> "France"),
array("id"=> 9, "name"=> "Lyon", "country"=> "France"),
array("id"=> 10, "name"=> "Bordeaux", "country"=> "France"),
array("id"=> 11, "name"=> "Montpellier", "country"=> "France"),
array("id"=> 16, "name"=> "Phoenix", "country"=> "United States"),
array("id"=> 17, "name"=> "San Antonio", "country"=> "United States"),
array("id"=> 18, "name"=> "San Diego", "country"=> "United States"),
array("id"=> 19, "name"=> "Dallas", "country"=> "United States"),
array("id"=> 20, "name"=> "San Jose", "country"=> "United States"),
array("id"=> 21, "name"=> "Jacksonville", "country"=> "United States"));
echo json_encode($data);
?>
现在,每次您按下一个键时,它都会执行该查询,您可以通过在 PHP 代码中获取 $_POST['query'] 来获取用户键入的任何内容。然后,您可以过滤数据或执行数据库查询或其他任何操作。
干杯