2009-11-11 47 views
0

我已经创建了添加表格行的代码,并尝试将第三方SuggestBox函数绑定到每个动态生成的表单字段。如何绑定和取消绑定第三方jQuery库函数?

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#form1').validationEngine(); 
    var newRowNum = 1; 
    $(".addRow").click(function(){ 
     var $newTr = $("#tb1 tbody>tr:last").clone(true); 
     $newTr.find('input[id^=foods]').unbind(jsonSuggest()); <== try to unbind the previouse jsonsuggest() 
     //$newTr.find('.jsonSuggestResults').remove(); 
     $newTr.appendTo("#tb1 tbody"); 
     $('input[id^=foods]', $newTr).val(''); 
     $newTr.find('input[id^=foods]').each(function(){ 
      $(this).jsonSuggest(
        function(text, wildCard, caseSensitive, notCharacter) { 
         rez = $.ajax({ 
          type: 'GET', 
          url: 'getFoodJSON.jsp', 
          data: 'foods=' + text, 
          dataType: 'json', 
          async: false 
         }); 
         return eval(rez.responseText); 
         }, 
         { ajaxResults:true 
         }); 
     }); 
     $newTr.find('input[id^=supplyDate]').each('id', function(){ 
      $(this).datepicker({dateFormat:'yy-mm-dd'}); 
     }); 
    }); 
}); 

然而,SuggestBox建议积累重复。下面是结果,当我输入的东西在7行...

link text

你介意告诉我如何解除绑定的前一行+表单字段应用的功能?谢谢。

回答

0

通过书写unbind(jsonSuggest()),你是调用jsonSuggest并解除它返回的值的绑定。除非jsonSuggest函数是一个返回处理程序方法(它可能不是)的生成器,这不是你想要的。如果是这样,那还不是你想要的,除非它每次都返回相同的处理程序。

你想(我假设)通过编写unbind(jsonSuggest)解除jsonSuggest函数本身,而没有括号。