2010-03-17 67 views
1

我有這樣的代碼:如何在Jquery Autocomplete上「取消綁定」.result?

$("#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很多次,沒有一次。

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(); 
    }); 
} 
+0

放在回調函數'$(本).VAL(「」);'指的輸入設置值設置爲「」。或$(「#xyz」)。val(「」); – ant 2010-03-17 13:10:41

+0

@ c0mrade這不起作用。謝謝 – Cesar 2010-03-17 13:20:47

回答

2

這樣做是爲了清除處理程序:

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

這是怎麼.result()內部工作:

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

所以你只想做反向並解除綁定

+0

您的答案可以刪除結果,但我的代碼還沒有工作。增加了示例代碼。 – Cesar 2010-03-17 13:39:10

0

也ü可以刷新緩存,此選擇:

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