Показать сообщение отдельно
  #5 (permalink)  
Старый 28.11.2017, 22:41
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,070

autocomplete country city
Patron,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <style type="text/css">
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
  <script>
$(function() {
var $_searchCountry = $("#search-country");
var country = {
    1: "Ukraine",
    2: "Germany",
    3: "France",
    4: "Spain",
    5: "Sweden",
    6: "USA",
    7: "Canada",
    8: "Moldova",
    9: "Belarus",
    10: "Poland"
};
$.ui.autocomplete.prototype._renderItem = function(ul, item) {
    var re = new RegExp($.ui.autocomplete.escapeRegex(this.term), "i");
    var t = item.label.replace(re, "<span style='font-weight:600;color:#FF0000;'>" + "$&" + "</span>");
    return $("<li></li>").append("<a>" + t + "</a>").appendTo(ul)
};
$_searchCountry.autocomplete({
    source: function(request, response) {
        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        response($.map(Object.keys(country), function(item) {
            return {
                value: country[item],
                label: country[item]
            }
        }).filter(function(item) {
            return matcher.test(item.label)
        }))
    }
});
var city = {
    "1": {
        "country": 1,
        "name": "Kiev"
    },
    "2": {
        "country": 3,
        "name": "Paris"
    },
    "3": {
        "country": 4,
        "name": "Madrid"
    },
    "4": {
        "country": 6,
        "name": "Houston"
    },
    "5": {
        "country": 7,
        "name": "Montreal"
    },
    "6": {
        "country": 8,
        "name": "Кишинев"
    },
    "7": {
        "country": 9,
        "name": "Minsk"
    },
    "8": {
        "country": 10,
        "name": "Warsaw"
    },
    "100": {
        "country": 1,
        "name": "Львов"
    },
    "101": {
        "country": 1,
        "name": "Николаев"
    },
    "103": {
        "country": 1,
        "name": "Переяслав-Хмельницкий"
    },
    "104": {
        "country": 1,
        "name": "Каменец-Подольский"
    },
    "105": {
        "country": 1,
        "name": "Donetsk"
    },
    "106": {
        "country": 1,
        "name": "Kharkov"
    },
    "107": {
        "country": 1,
        "name": "Луцк"
    },
    "108": {
        "country": 1,
        "name": "Poltava"
    },
    "109": {
        "country": 1,
        "name": "Черновцы"
    },
    "299": {
        "country": 1,
        "name": "Чернигов"
    },
    "333": {
        "country": 1,
        "name": "Чернигов"
    }
}
var $_searchCity = $("#search-city");
$_searchCity.autocomplete({
    source: function(request, response) {
        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        var land = $_searchCountry.val();
        land = Object.keys(country).filter(function(item) {
            return country[item] == land
        });
        var town = Object.keys(city).filter(function(item) {
            return city[item].country == land
        });
        response($.map(town, function(item) {
            return {
                value: city[item].name,
                label: city[item].name
            }
        }).filter(function(item) {
            return matcher.test(item.label)
        }))
    },
    minLength: 0
});
});
  </script>
</head>

<body>
<label>Search country:</label>
<input id="search-country" type="text" />
<label>Search city:</label>
<input id="search-city" type="text" />

</body>
</html>
Ответить с цитированием