您好我正在使用jquery select2-plugin:
http://ivaynberg.github.io/select2/
基本用法:
<select style="width:300px" id="source">
<optgroup label="Alaskan/Hawaiian Time Zone">
<option value="AK">Alaska</option>
<option value="HI">Hawaii</option>
</optgroup>
<optgroup label="Pacific Time Zone">
<option value="CA">California</option>
<option value="NV">Nevada</option>
<option value="OR">Oregon</option>
<option value="WA">Washington</option>
</optgroup>
</select>
$(document).ready(function () {
$("#source").select2({
placeholder: "Select a State",
allowClear: true
});
});
完美无缺, 但有人知道如何继续我需要“加载远程数据”,网址应该发送数据到PHP文件:
include "storescripts/connect_to_mysql.php";
$data = trim(strip_tags($_GET['data'])); // Like this?
if(!empty($data) && isset($data) && strlen($data) != 0) {
$stmt = $dbh->prepare('SELECT id,product FROM `table` WHERE `product` LIKE :keyword');
$keyword = "%".$data."%";
$stmt->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$stmt->execute();
while ($user = $stmt->fetch(PDO::FETCH_ASSOC)) {
$user['item']=htmlentities(stripslashes($user['product']));
$user['id']=$user['id'];
$user_set[] = $user;
}
echo json_encode($user_set);
} else {
echo json_encode(array("error" => "You forgot to type..." , "result" => 0));
}
echo json_encode($user_set);
有人知道如何为这个过程构建jquery / ajax吗? 我尝试了网站上的示例代码,但没有用,任何人都可以帮我解决这个问题吗?
问候!!
答案 0 :(得分:0)
Ajax调用示例:
$('#dropdown_users').select2({
placeholder: "Search User",
minimumInputLength: 3,
multiple: false,
ajax: {
url: "/ingeb/api_v1/users",
dataType: 'json',
quietMillis: 100,
data: function (term, page) {
return {
q: term,
page_limit: 10
};
},
results: function (data, page) {
return {results: data};
}
}
});
PHP返回值的格式应采用以下格式:
[{"id":2,"text":"kim gysen"}]
HTML如下:
<input type="hidden" id="dropdown_users" />
将输入类型设置为隐藏。