【问题标题】:updating data source in bootstrap typehead in javascript在 javascript 中的 bootstrap typeahead 中更新数据源
【发布时间】:2012-03-27 21:38:11
【问题描述】:

我希望下拉选项反映存储在名为 companyinfo.js 的文件中的数组中的所有项目,我通过 ajax 请求该文件。我在页面加载时调用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
        }
    });
}​

【问题讨论】:

    标签: javascript ajax twitter-bootstrap


    【解决方案1】:

    您不应该在 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
        }
    

    【讨论】:

    • 由于某种原因仍然无法正常工作。查看我上面的更新代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多