Home > Enterprise >  Update Select2's value while typing?
Update Select2's value while typing?

Time:10-13

In my country, we're using Cyrillic characters, instead of Latin. Many people don't have Cyrillic keyboard on their devices so I decided to use a Latin to Cyrillic function so they can find their city in the Select2's dropdown. The function works, but does not replace the last written letter. When I write a few letters and delete the last one it works. Maybe the function is delaying and activates after Select2's search?


Here's the code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" />
</head>

<body>
    <div class="container" style="margin-top: 20px;">
        <select id="city">
            <option value="">-- изберете град --</option>
            <option>Добрич</option>
            <option>Варна</option>
            <option>Бургас</option>
            <option>София</option>
        </select>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
    <script>
        $("#city").select2({
            width: "100%",
            allowClear: true,
            placeholder: "-- изберете град --"
        });

        $(document).on("focus", ".select2-search__field", function () {
            $(this).attr("autocomplete", "12525dcd27fc636c05a033507e1f2fdd");
        });

        $(document).on("keyup", ".select2-search__field", function () {
            var b = $(this).val();

            b = b.replace(/q/g, "я"), b = b.replace(/w/g, "в"), b = b.replace(/e/g, "е"), b = b.replace(/r/g, "р"), b = b.replace(/t/g, "т"), b = b.replace(/y/g, "ъ"), b = b.replace(/u/g, "у"), b = b.replace(/i/g, "и"), b = b.replace(/o/g, "о"), b = b.replace(/p/g, "п"), b = b.replace(/a/g, "а"), b = b.replace(/s/g, "с"), b = b.replace(/d/g, "д"), b = b.replace(/f/g, "ф"), b = b.replace(/g/g, "г"), b = b.replace(/h/g, "х"), b = b.replace(/j/g, "й"), b = b.replace(/k/g, "к"), b = b.replace(/l/g, "л"), b = b.replace(/z/g, "з"), b = b.replace(/x/g, "ь"), b = b.replace(/c/g, "ц"), b = b.replace(/v/g, "ж"), b = b.replace(/b/g, "б"), b = b.replace(/n/g, "н"), b = b.replace(/m/g, "м"), b = b.replace(/\`/g, "ч"), b = b.replace(/\[/g, "ш"), b = b.replace(/\]/g, "щ"), b = b.replace(/\\/g, "ю"), b = b.replace(/Q/g, "Я"), b = b.replace(/W/g, "В"), b = b.replace(/E/g, "Е"), b = b.replace(/R/g, "Р"), b = b.replace(/T/g, "Т"), b = b.replace(/Y/g, "Ъ"), b = b.replace(/U/g, "У"), b = b.replace(/I/g, "И"), b = b.replace(/O/g, "О"), b = b.replace(/P/g, "П"), b = b.replace(/A/g, "А"), b = b.replace(/S/g, "С"), b = b.replace(/D/g, "Д"), b = b.replace(/F/g, "Ф"), b = b.replace(/G/g, "Р"), b = b.replace(/H/g, "Х"), b = b.replace(/J/g, "Й"), b = b.replace(/K/g, "К"), b = b.replace(/L/g, "Л"), b = b.replace(/Z/g, "З"), b = b.replace(/X/g, "Ь"), b = b.replace(/C/g, "Ц"), b = b.replace(/V/g, "Ж"), b = b.replace(/B/g, "Б"), b = b.replace(/N/g, "Н"), b = b.replace(/M/g, "М"), b = b.replace(/\~/g, "Ч"), b = b.replace(/\{/g, "Ш"), b = b.replace(/\}/g, "Щ"), b = b.replace(/\|/g, "Ю");

            $(this).val(b);
        });
    </script>
</body>

</html>

CodePudding user response:

You could trigger input event instead of change

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" />
</head>

<body>
    <div class="container" style="margin-top: 20px;">
        <select id="city">
            <option value="">-- изберете град --</option>
            <option>Добрич</option>
            <option>Варна</option>
            <option>Бургас</option>
            <option>София</option>
        </select>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
    <script>
        $("#city").select2({
            width: "100%",
            allowClear: true,
            placeholder: "-- изберете град --"
        });

        $(document).on("focus", ".select2-search__field", function () {
            $(this).attr("autocomplete", "12525dcd27fc636c05a033507e1f2fdd");
        });

        $(document).on("keyup", ".select2-search__field", function () {
            var b = $(this).val();

            b = b.replace(/q/g, "я"), b = b.replace(/w/g, "в"), b = b.replace(/e/g, "е"), b = b.replace(/r/g, "р"), b = b.replace(/t/g, "т"), b = b.replace(/y/g, "ъ"), b = b.replace(/u/g, "у"), b = b.replace(/i/g, "и"), b = b.replace(/o/g, "о"), b = b.replace(/p/g, "п"), b = b.replace(/a/g, "а"), b = b.replace(/s/g, "с"), b = b.replace(/d/g, "д"), b = b.replace(/f/g, "ф"), b = b.replace(/g/g, "г"), b = b.replace(/h/g, "х"), b = b.replace(/j/g, "й"), b = b.replace(/k/g, "к"), b = b.replace(/l/g, "л"), b = b.replace(/z/g, "з"), b = b.replace(/x/g, "ь"), b = b.replace(/c/g, "ц"), b = b.replace(/v/g, "ж"), b = b.replace(/b/g, "б"), b = b.replace(/n/g, "н"), b = b.replace(/m/g, "м"), b = b.replace(/\`/g, "ч"), b = b.replace(/\[/g, "ш"), b = b.replace(/\]/g, "щ"), b = b.replace(/\\/g, "ю"), b = b.replace(/Q/g, "Я"), b = b.replace(/W/g, "В"), b = b.replace(/E/g, "Е"), b = b.replace(/R/g, "Р"), b = b.replace(/T/g, "Т"), b = b.replace(/Y/g, "Ъ"), b = b.replace(/U/g, "У"), b = b.replace(/I/g, "И"), b = b.replace(/O/g, "О"), b = b.replace(/P/g, "П"), b = b.replace(/A/g, "А"), b = b.replace(/S/g, "С"), b = b.replace(/D/g, "Д"), b = b.replace(/F/g, "Ф"), b = b.replace(/G/g, "Р"), b = b.replace(/H/g, "Х"), b = b.replace(/J/g, "Й"), b = b.replace(/K/g, "К"), b = b.replace(/L/g, "Л"), b = b.replace(/Z/g, "З"), b = b.replace(/X/g, "Ь"), b = b.replace(/C/g, "Ц"), b = b.replace(/V/g, "Ж"), b = b.replace(/B/g, "Б"), b = b.replace(/N/g, "Н"), b = b.replace(/M/g, "М"), b = b.replace(/\~/g, "Ч"), b = b.replace(/\{/g, "Ш"), b = b.replace(/\}/g, "Щ"), b = b.replace(/\|/g, "Ю");

            $(this).val(b);
            $(this).trigger('input');
        });
    </script>
</body>

</html>

  • Related