【发布时间】:2014-03-13 19:29:39
【问题描述】:
我想使用this jquery plugin 从数据库中获取值...
我创建 jquery ajax 代码和 HTML 以从数据库中获取值:
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<link href="http://ivaynberg.github.com/select2/select2-3.3.2/select2.css" rel="stylesheet" type="text/css" />
<script src="http://ivaynberg.github.com/select2/select2-3.3.2/select2.js"></script>
</head>
<body>
<select id="test" style="width:200px;">
<option value=""><option>
</select>
<script>
$('#test').select2({
ajax: {
dataType: "json",
url: "json.php",
results: function (data) {
return {results: data};
}
}
});
</script>
</body>
和json.php代码:
<?php
$pdo=new PDO("mysql:dbname=ddd;host=localhost","ddd","ddd");
$statement=$pdo->prepare("SELECT id,ime_prezime FROM radnici");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
echo $json;
?>
当我运行 php 代码时,我得到了 json:
[{"id":"1","ime_prezime":"Pera Peric"}]
问题不在于 php 代码...我的 html/jquery 代码有什么问题?
我什么都没有,我无法从 json.php 文件中获取值
更新:
我发现错误是 json 格式,但现在我无法保存我得到的值,所以当我单击值时就会消失...
<input id="test" style="width:300px;">
<select multiple id="test" style="width:300px"></select>
<script>
function formatValues(data) {
return data.ime_prezime;
}
$('#test').select2({
ajax: {
dataType: "json",
url: "json.php",
results: function (data) {
return {results: data};
}
},
formatResult: formatValues
});
</script>
【问题讨论】:
-
我也尝试过: 但我还是没有从数据库中获取值
-
如果您使用chrome,请右键单击->检查元素->网络选项卡并刷新页面并查看ajax的请求和响应
-
我更新了我的代码,但现在我无法选择它,因为当我选择并单击然后值只是 dessapear
标签: javascript php jquery mysql json