2012-03-27 1 views
0

私はajaxを介して要求するcompanyinfo.jsというファイルに格納された配列内のすべての項目を反映するためのドロップダウン・オプションを必要とします。ページが読み込まれたら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 
     } 
    }); 
} 

UPDATES:

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

何らかの理由でまだ動作していません。上の私の更新されたコードをチェックアウト –

関連する問題