开发者

how to use .live with autocomplete in jquery?

开发者 https://www.devze.com 2023-04-03 18:23 出处:网络
I want to use autocomplete with .live function, but it gives syntax error $(\"input[name=point]\").live(function() {

I want to use autocomplete with .live function, but it gives syntax error

$("input[name=point]").live(function() {
    $(this).autocomplete({
        minLength:0,   //for local data
        delay:0,      //for local data
        source:fu开发者_JAVA技巧nction(request,response){
            //var param= {"action":"getSalePoints"};
            $.getJSON("controllers/Order.controller.php?action=getSalePoints",request,function(result){
                //create array for response objects
                var suggestions = [];
                //process response
                $.each(result, function(i, val){                                
                    //alert(val.number);
                    suggestions.push(val.number);
                });

                //pass array to callback
                response(suggestions);
            });


        },
        select: function( event, ui ) {
            var param={
                "action":"getSalePointNo",
                "point":ui.item.value
                };
            $.getJSON("controllers/Order.controller.php",param,function(result){            
                if(result == "0"){
                    $('#resultMsg').attr('class','errorMsg');
                }
                else{
                    alert(result);
                    $('[name=pointNo]', $(this).parents(".bill")).val(no);
                }

            });
        }
    });
});


As 'mu is to short' suggested, you are not supplying the event type to the live function, you can try using 'focus' as the event type, try:

$("input[name=point]").live("focus", function() {
$(this).autocomplete({
    minLength:0,   //for local data
    delay:0,      //for local data
    source:function(request,response){
        //var param= {"action":"getSalePoints"};
        $.getJSON("controllers/Order.controller.php?action=getSalePoints",request,function(result){
            //create array for response objects
            var suggestions = [];
            //process response
            $.each(result, function(i, val){                                
                //alert(val.number);
                suggestions.push(val.number);
            });

            //pass array to callback
            response(suggestions);
        });


    },
    select: function( event, ui ) {
        var param={
            "action":"getSalePointNo",
            "point":ui.item.value
            };
        $.getJSON("controllers/Order.controller.php",param,function(result){            
            if(result == "0"){
                $('#resultMsg').attr('class','errorMsg');
            }
            else{
                alert(result);
                $('[name=pointNo]', $(this).parents(".bill")).val(no);
            }

        });
    }
});

});


The live function takes two arguments:

.live( eventType, handler )
Description: Attach a handler to the event for all elements which match the current selector, now and in the future.

You're not supplying the eventType so you'll be triggering an error inside jQuery as it tries to work with a function as a string and an undefined value as a function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号