-1

我有这样的脚本

$(function(){
    //attach autocomplete  
    $("#user_key").autocomplete({
            //define callback to format results  
                source: function(req, add){
                    //pass request to server  
                    $.getJSON("/ajax/user_autocomplete.php?user_key=?",req, function(data){
                        //create array for response objects
                        var suggestions = [];
                        //process response
                        $.each(data, function(i, val){
                        suggestions.push(val.name);  
                    });
                    //pass array to callback
                add(suggestions);
            });
        }
    });
});

并且想将一个参数发送到 php 文件以接收答案,我该如何在这个脚本中做到这一点?是否有必要在 html 中包含method="get"或没关系?

我必须写{param: $(this).val} 而不是 req或如何?

4

2 回答 2

0

Just do:

       $('#user_key').autocomplete(
        {
            source: '/ajax/user_autocomplete.php'
        });

As long as you send back correct JSON from user_autocomplete.php then all is good.

于 2012-08-03T15:53:04.847 回答
0

参数将req是一个包含属性的对象term。这和任何其他 GET 参数都需要为第二个参数定义$.getJSON

function(req, add){
                var params = {
                    user_key: myUserKeyVar,
                    term : req.term,
                    otherKey: myOtherKey
                };
                //pass request to server  
                $.getJSON("/ajax/user_autocomplete.php",params, function(data){
                    //create array for response objects
                    var suggestions = [];
                    //process response
                    $.each(data, function(i, val){
                    suggestions.push(val.name);  
                });
                //pass array to callback
            add(suggestions);
于 2012-08-03T14:22:00.407 回答