It is necessary instead of the usual text field to make a drop-down list with checkboxes. Something like this:
In this case, in the original form, the values ββfit in like this:
<input name="name" value="val1, val3" type="text"> That is, separated by a comma, and if I also noted the first paragraph, it would be fine. So that, for example, marking the 3rd value as the first one, it appeared at the top of the list, and the others in order.
Already riveted script:
$(document).ready(function() { // ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΡ var val_data = { "val1": "ΠΠ½Π°ΡΠ΅Π½ΠΈΠ΅ 1", "val2": "ΠΠ½Π°ΡΠ΅Π½ΠΈΠ΅ 2", "val3": "ΠΠ½Π°ΡΠ΅Π½ΠΈΠ΅ 3", "val4": "ΠΠ½Π°ΡΠ΅Π½ΠΈΠ΅ 4", "val5": "ΠΠ½Π°ΡΠ΅Π½ΠΈΠ΅ 5" }; var input = $('[name="other1"]'); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΎΠ±ΡΠΈΠΉ Π±Π»ΠΎΠΊ Ρ ΠΊΠ»Π°ΡΡΠΎΠΌ var val_cont = document.createElement('div'); $(val_cont).addClass("dropdown"); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΡ ΠΎΡΠΊΡΡΡΠΈΡ ΡΠΏΠΈΡΠΊΠ° ΠΈ ΠΏΠΎΠ»Π΅ Π΄Π»Ρ Π·Π°ΠΏΠΈΡΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ $(val_cont).append("<a href='javascript:void(0);'><span class='open'>ΠΡΠ±ΡΠ°ΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅</span><span class='value'></span></a>"); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Π²ΡΠΏΠ΄Π°ΡΡΠΈΠΉ ΡΠΏΠΈΡΠΎΠΊ ΠΈ Π²ΠΊΠ»Π°Π΄ΡΠ²Π°Π΅ΠΌ Π² ΠΎΠ±ΡΠΈΠΉ Π±Π»ΠΎΠΊ var ul = document.createElement('ul'); for (elem in val_data) { $(ul).append("<li><input type='checkbox' value='" + elem + "' id='" + elem + "'><label for='" + elem + "'>" + val_data[elem] + "</label></li>"); } $(ul).appendTo(val_cont); $(ul).hide(); // Π Π°Π·ΠΌΠ΅ΡΠ°Π΅ΠΌ ΠΎΠ±ΡΠΈΠΉ Π±Π»ΠΎΠΊ ΠΏΠΎΡΠ»Π΅ Π½ΡΠΆΠ½ΠΎΠ³ΠΎ input-Π° $(input).after(val_cont); // Π‘ΠΊΡΡΠ²Π°Π΅ΠΌ/ΠΎΡΠΊΡΡΠ²Π°Π΅ΠΌ Π²ΡΠΏΠ°Π΄Π°ΡΡΠΈΠΉ ΡΠΏΠΈΡΠΎΠΊ $(".dropdown a").on('click', function() { $(".dropdown ul").slideToggle('fast'); }); $('.dropdown ul input[type="checkbox"]').on('click', function() { var title_val = $(this).closest('.dropdown ul').find('input[type="checkbox"]').val(), title = $(this).val() + ", "; if ($(this).is(':checked')) { var html = '<span data-atr="' + title + '">' + title + '</span>'; $('.value').append(html); $(".open").hide(); } else { $('span[data-atr="' + title + '"]').remove(); } if ($('.value').text() == "") { $(".open").show(); $(input).val(""); } else { $(input).val($('.value').text()); } }); }); * { box-sizing: border-box; } body, ul { margin: 0; padding: 0; font-family: Arial; } input[type="text"] { display: none; } .dropdown { position: relative; display: table; margin: 10px auto; color: #333; min-width: 220px; } .dropdown a { display: block; text-decoration: none; color: #fff; background: #333; padding: 12px 20px 10px; border-radius: 20px; text-transform: uppercase; font-size: .95em; text-align: center; } .dropdown ul { position: relative; list-style: none; background: #eee; margin-top: 10px; padding: 5px 0; border-radius: 20px; font-size: .9em; } .dropdown ul:before { content: ''; position: absolute; right: 20px; bottom: 100%; width: 0; height: 0; border-style: solid; border-width: 0 7px 6px 7px; border-color: transparent transparent #eeeeee transparent; } .dropdown ul li { padding: 10px 14px; border-bottom: 1px solid #e0e0e0; } .dropdown ul li:last-child { border: none; } .dropdown input[type="checkbox"] { display: none; } .dropdown label { display: block; position: relative; padding-left: 22px; cursor: pointer; } .dropdown label:before { content: ''; position: absolute; left: 0; top: 0; width: 12px; height: 12px; border: 2px solid #666; border-radius: 4px; } .dropdown input[type="checkbox"]:checked ~ label:before { background: #666; } .dropdown label:after { content: ''; position: absolute; display: block; width: 5px; height: 12px; top: -5px; left: 6px; border: 2px solid #fff; box-shadow: 1px 2px 2px #555; border-top-color: transparent; border-left-color: transparent; -webkit-transform: rotate(45deg); transform: rotate(45deg); z-index: 2; opacity: 0; visibility: hidden; } .dropdown input[type="checkbox"]:checked ~ label:after { opacity: 1; visibility: visible; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="other1" value="val2, val5" type="text"> So. As we see from the example, everything works. However, it is necessary to check the script code itself, I am sure that it has jambs or can be shortened. The most important thing that canβt be dealt with in any way is, in general, this is the question: How to check the contents of the input and, if the values ββmatch, mark the necessary items in the drop-down list?
