首页 > 解决方案 > 键入时更新 Select2 的值?

问题描述

在我的国家,我们使用的是西里尔字符,而不是拉丁文。许多人的设备上没有西里尔字母键盘,所以我决定使用拉丁语到西里尔字母的功能,这样他们就可以在 Select2 的下拉列表中找到他们的城市。该功能有效,但不会替换最后一个书面字母。当我写几封信并删除最后一封时,它起作用了。也许该功能在 Select2 搜索后延迟并激活?


这是代码:

<!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/select2@4.1.0-rc.0/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/select2@4.1.0-rc.0/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>

标签: javascriptjqueryjquery-select2

解决方案


您可以触发input事件而不是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/select2@4.1.0-rc.0/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/select2@4.1.0-rc.0/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>


推荐阅读