JQuery 自动完成 1.8 中的附加搜索词

发布于 2024-12-09 12:55:26 字数 1882 浏览 0 评论 0原文

我是 Jquery 的新手,所以我可能会遗漏一些明显的东西......

我正在使用 JQuery 的 1.8 自动完成小部件进行搜索。用户输入 3 个字符后,该功能被触发。因此,下面函数中的“term”代表街道名称中的字符。

但是,用户已经输入了他们的邮政编码和门牌号,我希望这些值也传递给查询。我也如何发送这些值?我只能弄清楚如何发送单个“术语”。

(如果重要的话,我正在使用 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
    });

编辑:在帮助之后,下面是我的新代码,工作得很好!

我做了以下更新: 我的控制器操作被调用:

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

下面是更新的 Jquery。

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

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() }
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            value: item
                        }
                    }));
                }
            });
        },
        minLength: 3,
        delay: 0
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-12-16 12:55:26

ajax 请求的 data 参数接受 JSON 对象,因此您只需将其他属性添加到该对象即可。像这样:

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

在控制器操作中,您必须添加参数 zipCodehouseNumber

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文