如何绑定和解除第三方jQuery库函数的绑定?

时间:2009-11-11 02:54:45

标签: jquery jquery-plugins javascript-events

我创建了一个代码来添加一个带有表单字段的表行,并尝试将第三方SuggestBox函数绑定到每个动态生成的表单字段。

<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

您是否介意告诉我如何解除前一行+表单字段中应用函数的绑定?谢谢。

1 个答案:

答案 0 :(得分:0)

通过撰写unbind(jsonSuggest()),您调用 jsonSuggest并取消绑定它返回的值。除非jsonSuggest函数是返回处理程序方法的生成器(它可能不是),否则这不是你想要的。如果是,那仍然不是你想要的,除非它每次都返回相同的处理程序。

您希望(我假设)通过编写jsonSuggest来取消绑定unbind(jsonSuggest)函数本身,而不用括号。