jQuery 自动完成

发布于 2024-11-19 23:17:13 字数 2973 浏览 1 评论 0原文

我想为用户所在的城市构建一个 jquery 自动完成输入字段。当用户开始输入单词时,我想以这种格式显示具有最密切关联的 5 个城市的列表 [城市,国家]

我有三个表

  1. 国家(id,国家名称)
  2. 区域(id,区域名称,国家ID)
  3. 城市(id,城市名称,区域ID,国家ID)

当用户选择一个城市时,我想捕获城市的ID以将其插入数据库中。

我应该如何使用自动完成来做到这一点?

我应该使用城市的 id 设置隐藏字段还是传递城市的名称,然后检查其 id 进行选择是什么?这里的问题是全球各地都有同名的城市,并且会通过差异来识别它。

JS 代码

$("#UserCity").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "http://localhost/baku/users/location/",
            dataType: "json",
            data: {
                data: request.term
            },
            success: function (data) {
                /// Need to modify this part
                response($.map(data.geonames, function (item) {
                    return {
                        label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                        value: item.name
                    }
                }));
            }
        });
    },
    minLength: 2
});

示例 JSON 字符串我从 Ajax 获取

[{"name":"Banaras, Uttar Pradesh, India","city_id":"1927","region_id":"2193","country_id":"113"},{"name":"Banda, Uttar Pradesh, India","city_id":"1938","region_id":"2193","country_id":"113"},{"name":"Bangalore, Karnataka, India","city_id":"1949","region_id":"2185","country_id":"113"},{"name":"Banganapalle, Andhra Pradesh, India","city_id":"1950","region_id":"2169","country_id":"113"},{"name":"Banswara, Rajasthan, India","city_id":"1983","region_id":"2190","country_id":"113"},{"name":"Banur, Punjab, India","city_id":"1987","region_id":"2189","country_id":"113"}]

我想将名称显示为列表并通过表单将 City_id 作为可传递参数,这样我就不需要再次进行数据库查询。

阵列

Array
(
    [0] => Array
        (
            [name] => Banaras, Uttar Pradesh, India
            [city_id] => 1927
            [region_id] => 2193
            [country_id] => 113
        )

    [1] => Array
        (
            [name] => Banda, Uttar Pradesh, India
            [city_id] => 1938
            [region_id] => 2193
            [country_id] => 113
        )

    [2] => Array
        (
            [name] => Bangalore, Karnataka, India
            [city_id] => 1949
            [region_id] => 2185
            [country_id] => 113
        )

    [3] => Array
        (
            [name] => Banganapalle, Andhra Pradesh, India
            [city_id] => 1950
            [region_id] => 2169
            [country_id] => 113
        )

    [4] => Array
        (
            [name] => Banswara, Rajasthan, India
            [city_id] => 1983
            [region_id] => 2190
            [country_id] => 113
        )

    [5] => Array
        (
            [name] => Banur, Punjab, India
            [city_id] => 1987
            [region_id] => 2189
            [country_id] => 113
        )

)

I want to build a jquery Auto Complete input field for the city the user stays in. When the user starts typing in the words i wanna display a list of 5 cities with the closest associations in this format [City, Country]

i have three tables

  1. Country (id, country_name)
  2. Region (id, region_name, country_id)
  3. City (id, city_name, region_id, country_id)

when a user selects a city i want to capture the id of the City to insert it into the database.

How should i do this with Autocomplete ?

Should i set a hidden field with the id of the city or pass the name of the City and then check whats its id doing a select ? Problem here is there are cities across the globe with the same name and it will get diff to identify it.

JS code

$("#UserCity").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "http://localhost/baku/users/location/",
            dataType: "json",
            data: {
                data: request.term
            },
            success: function (data) {
                /// Need to modify this part
                response($.map(data.geonames, function (item) {
                    return {
                        label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                        value: item.name
                    }
                }));
            }
        });
    },
    minLength: 2
});

Example JSON String i get from Ajax

[{"name":"Banaras, Uttar Pradesh, India","city_id":"1927","region_id":"2193","country_id":"113"},{"name":"Banda, Uttar Pradesh, India","city_id":"1938","region_id":"2193","country_id":"113"},{"name":"Bangalore, Karnataka, India","city_id":"1949","region_id":"2185","country_id":"113"},{"name":"Banganapalle, Andhra Pradesh, India","city_id":"1950","region_id":"2169","country_id":"113"},{"name":"Banswara, Rajasthan, India","city_id":"1983","region_id":"2190","country_id":"113"},{"name":"Banur, Punjab, India","city_id":"1987","region_id":"2189","country_id":"113"}]

I wanna Display name as the list and take City_id as the passable parameter via the Form so that i dont need to again do a database query.

the Array

Array
(
    [0] => Array
        (
            [name] => Banaras, Uttar Pradesh, India
            [city_id] => 1927
            [region_id] => 2193
            [country_id] => 113
        )

    [1] => Array
        (
            [name] => Banda, Uttar Pradesh, India
            [city_id] => 1938
            [region_id] => 2193
            [country_id] => 113
        )

    [2] => Array
        (
            [name] => Bangalore, Karnataka, India
            [city_id] => 1949
            [region_id] => 2185
            [country_id] => 113
        )

    [3] => Array
        (
            [name] => Banganapalle, Andhra Pradesh, India
            [city_id] => 1950
            [region_id] => 2169
            [country_id] => 113
        )

    [4] => Array
        (
            [name] => Banswara, Rajasthan, India
            [city_id] => 1983
            [region_id] => 2190
            [country_id] => 113
        )

    [5] => Array
        (
            [name] => Banur, Punjab, India
            [city_id] => 1987
            [region_id] => 2189
            [country_id] => 113
        )

)

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

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

发布评论

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

评论(2

紫竹語嫣☆ 2024-11-26 23:17:13

HTML:

<input type="text" name="city" id="city" />
<input type="hidden" name="city_id" id="city_id" />

JQuery:

$('input.city').autocomplete('/ajax/autocomplete-city.php').result(function(e, data){
    $('city_id'.val(data[1]);
});

PHP:

$query = mysql_real_escape_string($_GET['q']);

$rs = mysql_query("
    SELECT
        CONCAT_WS(', ', city.city_name, region.region_name, country.country_name)
    ,   city.id
    FROM
        city
    INNER JOIN
        region ON region.id = city.region_id
    INNER JOIN
        country ON country.id = city.country_id
    WHERE
        city.city_name LIKE '$query%'
    LIMIT 10
");

while ($r = mysql_fetch_row($rs))
{
    echo implode("|", $r);
    echo "\n";
}

HTML:

<input type="text" name="city" id="city" />
<input type="hidden" name="city_id" id="city_id" />

JQuery:

$('input.city').autocomplete('/ajax/autocomplete-city.php').result(function(e, data){
    $('city_id'.val(data[1]);
});

PHP:

$query = mysql_real_escape_string($_GET['q']);

$rs = mysql_query("
    SELECT
        CONCAT_WS(', ', city.city_name, region.region_name, country.country_name)
    ,   city.id
    FROM
        city
    INNER JOIN
        region ON region.id = city.region_id
    INNER JOIN
        country ON country.id = city.country_id
    WHERE
        city.city_name LIKE '$query%'
    LIMIT 10
");

while ($r = mysql_fetch_row($rs))
{
    echo implode("|", $r);
    echo "\n";
}
聽兲甴掵 2024-11-26 23:17:13

当面对两个实例具有相同名称的可能性时,我总是添加一个独特的东西来区分它们(但这只是为了让用户能够做出正确的选择)。通常,您的城市应该有不同的 ID(它们应该是唯一的)。

When faced with the possibility of 2 instances having the same name, I always add one more unique thing, to differentiate between them ( but this is only so that the users can make the right choice ). Normally, your cities should have different IDs ( they should be unique ).

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