我有麻煩填充選定的插件多個獲取來自ajax調用的數據。我厭倦了在按照以下職位,填充選擇多個獲取插件與ajax請求
Jquery Chosen plugin - dynamically populate list by Ajax
Multiple Select - Chosen jQuery
Jquery chosen ajax call populate multiselelect not working
但並沒有幫助。這些數據只是沒有得到填補:(我的Ajax請求如下,
<script type="text/javascript" lang="javascript">
function doGetTag() {
alert('here');
$.ajax({
url: 'index.php/rest/resource/qtag',
//data: data,
success: function(data) {
var jsonObj = JSON.parse(data);
var tags = "";
var curVal = document.getElementById('tags').innerHTML;
for(var i = 0; i < jsonObj.length; i++) {
var tagObj = jsonObj[i];
//document.write("<option>" + tagObj.tagName + "</option>");
var tagHtml = "<option>" + tagObj.tagName + "</option></br>";
tags = tags + tagHtml ;
}
tagTotal = curVal + tags;
document.getElementById('tags').innerHTML = tagTotal;
alert(document.getElementById('tags').innerHTML);
},
type: "get"
});
}
</script>
它返回一個JSON字符串。如果我提醒它在一個消息框,得到的數據正確顯示在這裏。但問題是如何填充多個GET插件?以下是我的HTML,
<select data-placeholder="Tag your question here" style="width:350px;height:50px;" multiple class="chosen-select" id="tags">
<option value="" ></option>
</select>
我很新的這個插件,並會非常感謝你的幫助:)
FYI
我這樣做是使用PHP直接如下,
<select data-placeholder="Tag your question here" style="width:350px;height:50px;" multiple class="chosen-select" id="tags">
<option value="" ></option>
<?php
$con=mysqli_connect("localhost","user","pass","db");
$result = mysqli_query($con,"SELECT * FROM tags");
while($row = mysqli_fetch_array($result))
{
echo"<option>".$row['tagName']."</option>";
echo"</br>";
}
?>
</select>
,並正確顯示數據,但該項目的要求規定它是一個MUST使用AJAX請求來填充數據。非常感謝你:)你的專家意見是非常讚賞:)