我想在我的选择框中添加“选择的jQuery插件”。这里的数据来自谷歌电子表格。 我在我的函数下尝试了下面的代码来追加列表。但这不起作用。
Option = "<li class="active-result">" + this.gsx$list.$t + "</li>";
$('ul.chosen-results').append(Option);
这是我的demo
function createLIST(){
// url to spreadsheet
var url = "https://spreadsheets.google.com/feeds/list/1a00YuGgCNuzYfw7C4qxvpdlbRRiDHV45gPWwQ7E6X0E/o11yyjo/public/values?alt=json";
//var url = "https://spreadsheets.google.com/feeds/list/" + spreadsheetID + "/o11yyjo/public/values?alt=json";
$.getJSON(url, function(data) {
var entry = data.feed.entry;
//function(){$('ul.chzn-results').empty();},
$(entry).each(function(){
// add each option
//Option = "<li class="active-result">" + this.gsx$list.$t + "</li>";
//$('ul.chosen-results').append(Option);
Option = "<option>" + this.gsx$list.$t + "</option>";
$('#products-list').append(Option);
});
});
}
$(document).ready(function(e){
// now add the GoogleSpread sheet list
createLIST()
//$('#products-list').chosen();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.1.0/chosen.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.1.0/chosen.jquery.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<datalist id='products'>
</datalist>
<select id='products-list'>
</select>
答案 0 :(得分:1)
仅在从JSON响应中添加选项后初始化所选插件。
function createLIST() {
var url = "https://spreadsheets.google.com/feeds/list/1a00YuGgCNuzYfw7C4qxvpdlbRRiDHV45gPWwQ7E6X0E/o11yyjo/public/values?alt=json";
$.getJSON(url, function(data) {
var entry = data.feed.entry;
$(entry).each(function() {
Option = "<option>" + this.gsx$list.$t + "</option>";
$('#products-list').append(Option);
});
// initialize chosen after options are added
$('#products-list').chosen();
});
}
$(document).ready(function(e) {
createLIST();
});
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.1.0/chosen.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.1.0/chosen.jquery.js"></script>
<datalist id='products'>
</datalist>
<select id='products-list'>
</select>
&#13;