如何在jquery中使用自动完成的.live?

时间:2011-09-10 08:24:53

标签: jquery autocomplete live

我想在.live函数中使用自动完成功能,但它会出现语法错误

$("input[name=point]").live(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);
                }

            });
        }
    });
});

2 个答案:

答案 0 :(得分:6)

由于'mu is to short'建议,你没有向live函数提供事件类型,你可以尝试使用'focus'作为事件类型,试试:

$("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);
            }

        });
    }
});

});

答案 1 :(得分:1)

live功能需要two arguments

  

<强> .live( eventType, handler )
  说明:为现在和将来与当前选择器匹配的所有元素附加处理程序。

你没有提供eventType因此你会在jQuery中触发错误,因为它试图将函数作为字符串和undefined值作为函数使用。