开发者

Additional search terms in JQuery Autocomplete 1.8

开发者 https://www.devze.com 2023-04-12 01:29 出处:网络
I\'m a newbie with Jquery, so I may be missing something obvious... . I am using JQuery\'s 1.8 autocomplete widget for a search search.After the user enters 3 characters the function is triggered.So

I'm a newbie with Jquery, so I may be missing something obvious... .

I am using JQuery's 1.8 autocomplete widget for a search search. After the user enters 3 characters the function is triggered. So "term" in the below function represents the characters in the street name.

However, the user will have already input their zip code and house number, and I would like those values to be passed to the query as well. How to I send those values, too? I can only figure out how to send the single "term".

(If it matters, I'm using ASP.Net MVC3)

  $("#SearchStreet").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/VoterAndPollingPlaceLookup/PollingPlaceSearch/FindStreet",
                dataType: "json",
                data: {
                    term: request.term
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            value: item
                        }
                    }));
                }
            });
        },
        minLength: 3,
        delay: 0
    });

EDIT: after help, the below is my new code that works just fine!

I have made the following updates: My controller action that is being called:

Public Function FindStreet(term As String, searchZip As String, searchHouse As String) As JsonResult
           .....returns results
        End Function

Below is the updated Jquery.

 $("#SearchStreet").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/OnlineVoterRegistration/ApplicationDetails/FindStreet",
                dataType: "json",
                data: { term: request.term, 
                        searchZip: $("#SearchZip").val(), 
                        searchHouse: $("#SearchHouse").val() }
                su开发者_开发知识库ccess: function (data) {
                    response($.map(data, function (item) {
                        return {
                            value: item
                        }
                    }));
                }
            });
        },
        minLength: 3,
        delay: 0
    });


The data parameter for the ajax request accepts a JSON object so all you have to do is add the other properties to that object. Like this:

$("#SearchStreet").autocomplete({ 
        source: function (request, response) { 
            $.ajax({ 
                url: "/VoterAndPollingPlaceLookup/PollingPlaceSearch/FindStreet", 
                dataType: "json", 
                data: { 
                    term: request.term,
                    zipCode: 'zip code',
                    houseNumber: 'house number'
                }, 
                success: function (data) { 
                    response($.map(data, function (item) { 
                        return { 
                            value: item 
                        } 
                    })); 
                } 
            }); 
        }, 
        minLength: 3, 
        delay: 0 
    });

In your controller action you have to add the parameters zipCode and houseNumber.

0

精彩评论

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

关注公众号