2014-03-28 54 views
0

我想用typeahead.js 0.9.3 https://github.com/twitter/typeahead與遠程選項,基本上我想申請自動完成5-6領域,我想添加動態值基於我的遠程url和valuekey中當前字段的id。Twitter typehead自動完成遠程源與動態值

這就是我想要的。

jQuery(function($) { 
$(".posts_autocomplete").each(function() { 
    var fid = $(this).attr("id"); 
    var field = fid.replace("post_", ""); 
    $(this).typeahead({ 
    name: 'posts_search', 
    remote: '/posts/search?q=%QUERY&field='+field, 
    valueKey: field, 
    minLength: 2, 
    limit: 10 
    }); 
}); 
}); 

我也試過替換Bootstrap 3 typeahead.js - remote url attributes但它似乎沒有工作。

回答

0

在每次迭代中使用針對打頭的唯一名稱解決此問題。

jQuery(function($) { 
$(".posts_autocomplete").each(function(i) { 
    var fid = $(this).attr("id"); 
    var field = fid.replace("post_", ""); 
    $("#"+fid).typeahead({ 
    name: i, 
    remote: '/posts/search?q=%QUERY&field='+field, 
    valueKey: field, 
    minLength: 2, 
    limit: 10 
    }); 
}); 
});