JQuery Ajax自动完成 - 响应事件未触发

时间:2014-05-09 18:47:53

标签: jquery autocomplete

下面是我的代码,自动完成工作正常,但我想在自动完成下拉列表中添加默认文本,我按照以下线程执行 how to display default(static) text to jquery autocomplete dropdown

实际执行情况略有不同,我无法得到响应

var options, a;
jQuery(function() {
    var __response = $.ui.autocomplete.prototype._response;
    $.ui.autocomplete.prototype._response = function(content) {
        __response.apply(this, [content]);
        this.element.trigger("autocompletesearchcomplete", [content]);
    };

    a = $('#txtOccupation').autocomplete({
        serviceUrl: '/App_Handlers/GetAjaxSuggestions.ashx?datasets=occ',
        minChars: 1,
        delimiter: /(,|;)\s*/,
        deferRequestBy: 0, //miliseconds
        noCache: false,
        width: 420,
        onSelect: function(value, data) {
            alert('You selected: ' + value + ', ' + data);
        },
        response: function(e, ui) {
            ui.content.unshift({label: "OR select an occupation:", value: "OR select an occupation:"});
        }
    }).bind("autocompletesearchcomplete", function(event, contents) {
        alert(contents.length);
    });
    browser = jQuery.browser;
    $('.autocomplete').css('padding-left', '10px');
});

所以我试图绑定一个事件来覆盖响应,但是它的抛出错误 $。ui.autocomplete未定义

正在加载应用程序

但是我的xslt中有引用(一切都在xslt中,jason字符串是从处理程序接收的自动完成)。

下面的代码工作正常,但文本没有添加

var options, a;
jQuery(function()
{
    a = $('#txtOccupation').autocomplete({
        serviceUrl: '/App_Handlers/GetAjaxSuggestions.ashx?datasets=occ',
        minChars: 1,
        delimiter: /(,|;)\s*/,
        deferRequestBy: 0, //miliseconds
        noCache: false,
        width: 420,
        onSelect: function(value, data) {
            alert('You selected: ' + value + ', ' + data);
        },
        response: function(e, ui) {
            ui.content.unshift({label: "OR select an occupation:", value: "OR select an occupation:"});//not working...
        }
    });
    browser = jQuery.browser;
    $('.autocomplete').css('padding-left', '10px');
});

JSON格式

{
    "query": 're',
    "suggestions": ['Real Estate Brokers', 'Real Estate Sales Agents'],
    "data": ['41902100', '41902200']
}

1 个答案:

答案 0 :(得分:1)

试试这个

jQuery(function() {
   $('#txtOccupation').autocomplete({
        serviceUrl: '/App_Handlers/GetAjaxSuggestions.ashx?datasets=occ',
        minChars: 1,
        delimiter: /(,|;)\s*/,
        deferRequestBy: 0, //miliseconds
        noCache: false,
        width: 420,
        onSelect: function(value, data) {
              alert('You selected: ' + value + ', ' + data);
        },beforeRender: function (container) {
              container.prepend("<div class='autocomplete-suggestion'>OR select an occupation:</div>");
        }
    });
});