dongyanju1094 2015-03-25 08:29
浏览 49

针对ajax函数的typeahead bootstrap自动建议

I'm using typeahead auto suggestion for my application but the problem is ajax function exists in bookingAjaxFunc function and bookingAjaxFunc code is

function bookingAjaxFunc(url, bookingId, jsonCall) {
     switch(jsonCall) {
          case 'Add':
               values = $('.booking-form').serializeArray();
          break;
          case 'autoSuggestion':
               values = {bookingId: bookingId, jsonCall: jsonCall};
          break;
     }
     $.ajax({
          url: url,
          type: 'POST',
          dataType: 'json',
          data: values,
          cache: false,
          success: function(data) {
               switch(jsonCall) {
                    case 'Add':
                         console.log(data);
                    break;
                    case 'autoSuggestion':
                         console.log(data);
                    break
               }
          }
     });
}

this is my ajax function which is working fine in insert, update, delete and other cases but now I want to call this function in typeahead how could I do this? and my typeahead function is working fine in predefined values but now I want to call my bookingAjaxFunc I couldn't figure out where to call my function, and remember this function is copy from an example and I paste it here as it as.

function substringMatcher(strs) {
    return function findMatches(q, cb) {
        var matches, substrRegex;

        // an array that will be populated with substring matches
        matches = [];

        // regex used to determine if a string contains the substring `q`
        substrRegex = new RegExp(q, 'i');

        // iterate through the pool of strings and for any string that
        // contains the substring `q`, add it to the `matches` array
        $.each(strs, function (i, str) {
            if (substrRegex.test(str)) {
                // the typeahead jQuery plugin expects suggestions to a
                // JavaScript object, refer to typeahead docs for more info
                matches.push({ value: str });
            }
        });

        cb(matches);
    };
}

var states = ['Alabama', 'Alaska'];

$('.autoSuggestion').typeahead({
    hint: true,
    highlight: true,
    minLength: 1
},
{
    name: 'states',
    displayKey: 'value',
    source: substringMatcher(states)
});

The bookingAjaxFunc function which create for my ajax call so where I use my this code for passing values to my function and use my case

var url = $("#do").val();
bookingAjaxFunc(url, '', 'autoSuggestion');

I also try to add remote below source but didn't work.

Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 关于#python#的问题:自动化测试