在javascript中更新引导打印头中的数据源
我希望下拉选项反映存储在名为companyinfo.js的文件中的数组中的所有项,我通过ajax请求该文件。当页面加载时,我调用在javascript中更新引导打印头中的数据源,javascript,ajax,twitter-bootstrap,Javascript,Ajax,Twitter Bootstrap,我希望下拉选项反映存储在名为companyinfo.js的文件中的数组中的所有项,我通过ajax请求该文件。当页面加载时,我调用dropDownList() function dropDownList (evt) { console.log("dropdownfired"); var companyArray = []; $.ajax({ url : 'assets/data/companyarray.js', //this file is just ["
dropDownList()
function dropDownList (evt) {
console.log("dropdownfired");
var companyArray = [];
$.ajax({
url : 'assets/data/companyarray.js', //this file is just ["Facbeook", "Twitter", "Klout",]
dataType: 'script',
success: function(data) {
companyArray = data;
console.log(companyArray); //returns an array of the companies
$('#companyInput1').attr('data-source', companyArray); //#companyInput1 is the input field where I want the typehead to be
console.log($('#companyInput1').attr('data-source')); //returns undefined
}
});
}
更新:
function dropDownList (evt) {
console.log("dropdownfired");
var companyArray = [];
$.ajax({
url : 'assets/data/companyarray.js', //this file is just ["Facbeook", "Twitter", "Klout",]
dataType: 'script',
success: function(data) {
companyArray = data;
console.log(companyArray); // gives array of companies
$('#companyInput1').data('data-source', companyArray);
console.log($('#companyInput1').data('data-source')); // gives undefined still
}
});
}
您不应该在HTML节点上存储字符串以外的任何内容作为属性。要存储数组,请使用jquery提供的
success: function(data) {
companyArray = data;
console.log(companyArray); //returns an array of the companies
$('#companyInput1').data('data-source', companyArray); //#companyInput1 is the input field where I want the typehead to be
console.log($('#companyInput1').data('data-source')); //should work
}
出于某种原因,这仍然不起作用。查看我上面更新的代码