我正在尝试在我的选择框中添加“Chosen jQuery plugin”。这里的数据来自谷歌电子表格。 我在我的函数下尝试了下面的代码来附加列表。但这不起作用。
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>
最佳答案
仅在从 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();
});
<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>
关于javascript - 带有 "Chosen jQuery plugin"的选择框中的 Google 电子表格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39098930/