如何“取消绑定”。关于Jquery自动完成的结果?

时间:2010-03-17 13:04:04

标签: jquery autocomplete callback

我有这段代码:

$("#xyz").unautocomplete().autocomplete(dataVar, {
    minChars: 0,
    width: 400,
    matchContains: true,
    highlightItem: true,
    formatItem: formatItem,
    formatResult: formatResult
})
.result(findValueCallback).next().click(function() {
    $(this).prev().search();
});

我多次调用此代码,第一次调用正常,但之后 他多次拨打findValueCallback,而不是再次。{/ p>

unautocomplete不清除.result

我需要做一次电话findValueCallback吗?

示例代码:

var niveis01 = [];
var niveis02 = [];
var niveis03 = [];

$(document).ready(function(){
    carregaDadosNivel1();
});

function carregaDadosNivel1() {
    $.ajax({
        url: "http://.....",
        cache: true,
        type: "POST",
        dataType:"json",
        success: function(data){
            ...
            niveis01 = data;
            habilitaComboNivel1();
            ...
        },
        error: function(xhr, ajaxOptions, thrownError){
            ...
        }
    });
}

function habilitaComboNivel1() {
    function findValueCallback1(event, data01, formatted) {
        ...
        carregaDadosNivel2();
        ...
    }

    $("#nivel01").unautocomplete().autocomplete(niveis01, {
        minChars: 0,
        width: 400,
        matchContains: true,
        highlightItem: true,
        formatItem: formatItem,
        formatResult: formatResult
    }).result(findValueCallback1).next().click(function() {
        $(this).prev().search();
    });
}

function carregaDadosNivel2() {
    $.ajax({
        url: "http://.....",
        cache: true,
        type: "POST",
        dataType:"json",
        success: function(data){
            ...
            niveis02 = data;
            habilitaComboNivel2();
            ...
        },
        error: function(xhr, ajaxOptions, thrownError){
            ...
        }
    });
}

function habilitaComboNivel2() {
    function findValueCallback2(event, data02, formatted) {
        ...
        carregaDadosNivel3();
        ...
    }

    $("#nivel02").unautocomplete().autocomplete(niveis02, {
        minChars: 0,
        width: 400,
        matchContains: true,
        highlightItem: true,
        formatItem: formatItem,
        formatResult: formatResult
    }).result(findValueCallback2).next().click(function() {
        $(this).prev().search();
    });
}

function carregaDadosNivel3() {
    $.ajax({
        url: ""http://.....",
        cache: true,
        type: "POST",
        dataType:"json",
        success: function(data){
            ...
            niveis03 = data;
            habilitaComboNivel3();
            ...
        },
        error: function(xhr, ajaxOptions, thrownError){
            ...
        }
    });
}

function habilitaComboNivel3() {
    function findValueCallback3(event, data03, formatted) {
        ...
    }

    $("#nivel03").unautocomplete().autocomplete(niveis03, {
        minChars: 0,
        width: 400,
        matchContains: true,
        highlightItem: true,
        formatItem: formatItem,
        formatResult: formatResult
    }).result(findValueCallback3).next().click(function() {
        $(this).prev().search();
    });
}

2 个答案:

答案 0 :(得分:2)

执行此操作以清除处理程序:

$("#xyz").unbind('result');

这就是.result()内部运作的方式:

result: function(handler) {
    return this.bind("result", handler);
}

所以你只想反过来取消绑定

答案 1 :(得分:0)

你也可以为这个选择器刷新缓存:

$("#xyz").flushCache();