2012-03-27 32 views
0

我想下拉选项来反映存储在名为companyinfo.js,我通过ajax请求的文件中存储的数组中的所有项目。当页面加载时,我打电话给dropDownList()更新数据源中的自举类型JavaScript中的数据源

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 
     } 
    }); 
}​ 

回答

0

你不应该存储除了字符串作为HTML节点属性的东西。用于存储阵列使用.data()-method 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 
    } 
+0

仍然心不是出于某种原因的工作。看看我上面更新的代码 – 2012-03-27 22:15:15