简体   繁体   English

Bootstrap Typeahead AJAX

[英]Bootstrap Typeahead AJAX

I'm using a modified version of typeahead for Twitter Bootstrap ( https://github.com/biggora/bootstrap-ajax-typeahead ), which simplifies using remote data. 我在Twitter Bootstrap( https://github.com/biggora/bootstrap-ajax-typeahead )中使用了typeahead的修改版本,该版本简化了远程数据的使用。 The problem I've run into is that my AJAX call url depends on the selected option in my select input. 我遇到的问题是我的AJAX调用网址取决于我选择输入中的选择选项。

var subjectId = $('#chapters-open-subject option:selected').val();      
    $('#chapters-edit-title').typeahead({
        onSelect: function(item){
            $('#chapters-edit-submit').attr('disabled',false).removeClass('btn-default').addClass('btn-primary');
        },
        ajax: {
            url: '/admin/misc/chapters/search/'+subjectId
        },
        displayField: 'naslov'
    });

The problem is that even though I change the option in my select box, the url in AJAX request stays the same and is not changed accordingly. 问题是,即使我更改了选择框中的选项,AJAX请求中的URL也保持不变,并且没有相应地更改。 How could I resolve this issue? 我该如何解决这个问题?

You might be able to get away with something like this: 也许可以摆脱像这样的东西:

var subjectId = $('#chapters-open-subject option:selected').val();

var $typeahead = $('#chapters-edit-title').typeahead({
    onSelect: function(item){
        $('#chapters-edit-submit').attr('disabled',false).removeClass('btn-default').addClass('btn-primary');
    },
    ajax: {
        url: '/admin/misc/chapters/search/' + subjectId
    },
    displayField: 'naslov'
});

$('#chapters-open-subject').on('change', function() {
    subjectId = $(this).val();
    $typeahead.data('typeahead').options.ajax.url = '/admin/misc/chapters/search/' + subjectId;
});

If that doesn't work, I'd suggest posting an issue with the library on GitHub. 如果那行不通,我建议在GitHub上发布该库的问题。 It does not seem to have the capability to change the URL easily after it is set. 设置后,它似乎不具备轻松更改URL的功能。

ajax : 
{
  preDispatch : function() { return { q: $("#subjectId").val() }},
  url: '/admin/misc/chapters/search/'+ ,
  method : 'get',
  displayField : 'naslov',
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM