Ajax Autocomplete for Jquery : How To Send Dynamic Parameters

前端 未结 6 1285
感情败类
感情败类 2021-01-17 22:03

i am using Ajax Autocomplete for Jquery ( http://www.devbridge.com/projects/autocomplete/jquery/ ) in one of my application. The Search Form looks something lik

6条回答
  •  野的像风
    2021-01-17 22:37

    All the answers above did NOT work for me (I suspect because I'm passing "POST" data through ajaxSettings variable which might affect "params" but I did not double check).

    Anyway all comments gave good hints and I came up with the following:

    var data={          
        'phrase': function() {
            return $('#ajax_post').val();
        },
        'activity_id': function() {
            return $('#activity_id').val();
        }
    }
    
    var options = {
        noCache: true,
        transformResult: function(response) {
            //whatever
        },
        onSelect: function(suggestion) {
            //whatever
        },  
        serviceUrl: "MyPHP.php",
        ajaxSettings: {
            dataType: "json",
            method: "POST",
            data: data
        }
    };  
    
    $("#ajax_post").autocomplete(options);
    

    So I was able to deliver to the PHP script both the content of ajax_post and activity_id input fields.

    Note that it's not enough to do:

    'phrase': $('#ajax_post').val()
    

    Because as mentioned in other answers the value get static.

提交回复
热议问题