简体   繁体   中英

How to bind and unbind a third party jQuery library function?

I've created code to add a table row with a form field, and try to bind the 3rd party SuggestBox function to each dynamically generated form field.

<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'});
        });
    });
});

However, the SuggestBox suggestions accumulate duplicates. Here is the result when I input something at row 7...

link text

Would you mind to telling me how to unbind the applied function at the previous row+form field? Thank you.

By writing unbind(jsonSuggest()) , you are calling jsonSuggest and unbinding the value that it returns. Unless the jsonSuggest function is a generator that returns a handler method (which it probably isn't), that's not what you want. If it is, that's still not what you want, unless it returns the same handler every time.

You want (I assume) to unbind the jsonSuggest function itself by writing unbind(jsonSuggest) , without parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM