Typeahead不会读取初始页面加载后创建的属性

时间:2015-05-12 13:37:05

标签: jquery ajax bootstrap-typeahead

我正在使用bootstrap-typeahead.js v2.0.3和JQuery v1.7.2。

我有一个从Bootstrap模式加载的表单,有两个需要使用typeahead的文本框。

<input id="filter___hotspot_id" class="filter_typeahead required_filter" data-filter-context="name" data-filter-tablename="hotspots" data-filter-columnname="name" style="width: 200px" autocomplete="off" type="text">
<input id="filter__interface" class="filter_typeahead required_filter" data-filter-context="default" data-filter-tablename="hotspot_interfaces" data-filter-columnname="name" data-filter-additional-col="hotspot_id" data-filter-additional-id="11" style="width: 200px" autocomplete="off" type="text">

根据在第一个文本框中输入的数据,第二个框中会添加两个属性(上面filter__interface项中的data-filter-additional-col和data-filter-additional-id属性)。使用这段代码就可以了。

$( document ).on( 'change', '#filter___hotspot_id', function () {
    var interface_filter = $('#filter__interface');
    var id_parts = $( this ).val().split( ' id:' );
    interface_filter.attr('data-filter-additional-col', 'hotspot_id' );
    interface_filter.attr('data-filter-additional-id', id_parts[id_parts.length - 1] );
    // This is the call to a function to reload the code where the ajax call is below in this question:
    FUNC.typeahead.init();
});

最初加载页面时会加载调用typeahead的ajax函数:

    $( p_field_obj )
        .typeahead({
            ajax: {
                url: 'ajax_handler.php?command=ajax-typeahead&tablename=' + 
                        $( p_field_obj ).attr( 'data-filter-tablename' ) + 
                        '&columnname=' + $( p_field_obj ).attr( 'data-filter-columnname' ) + 
                        '&context=' + $( p_field_obj ).attr( 'data-filter-context' ) +
                        '&additional_col=' + $(p_field_obj).attr('data-filter-additional-col') +
                        '&additional_id=' + $(p_field_obj).attr('data-filter-additional-id'),
                timeout: 300,
                triggerLength: 2,
                loadingClass: 'search_in_progress',
                preProcess: function( data ) {
                    retval = jQuery.parseJSON( data );
                    return retval;
                } 
            },
            matcher: function( item ) { return item; },  // Stops the client filtering the results coming back from AJAX.
            items: 999  // We rely on the AJAX server handler to return the right number of matches (ie LIMIT on the SQL query).
        });

问题是当从第二个文本框调用typeahead函数时,typeahead所在的上下文不会看到添加的属性。如何调用typeahead函数时,如何使添加的属性的值可用?

感谢任何帮助。谢谢!

0 个答案:

没有答案