【问题标题】:How to bind and unbind a third party jQuery library function?如何绑定和取消绑定第三方 jQuery 库函数?
【发布时间】:2009-11-11 02:54:44
【问题描述】:

我创建了代码来添加带有表单域的表格行,并尝试将第 3 方 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

您介意告诉我如何取消绑定前一行+表单字段中的应用函数吗?谢谢。

【问题讨论】:

    标签: jquery jquery-plugins jquery-events


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      相关资源
      最近更新 更多