Программирование на jquery
Anonymous
JQuery Multi Select Doplowddation с флажком
Сообщение
Anonymous » 16 май 2025, 14:55
Я пытаюсь достичь Multi Select Multi Select с флажками с настройкой кнопкой Apply и отменить кнопку внутри раскрывающегося списка. Когда я выбираю «Выбрать All < /code>», выпадает неожиданно закрывается, даже я пытался использовать StopPropagation < /code>, но все же он закрывает выпадающий список. Lang-HTML PrettyPrint-Override ">
Код: Выделить всё
MultiSelect with Fixed Select All
Select Options
Apple
Banana
Cherry
Date
< /code>
< /div>
< /div>
< /p>
$(document).ready(function () {
$('#my-select').multiselect({
placeholder: 'Select fruits',
onControlOpen: function () {
const $optionsContainer = $('.ms-options');
// Add Select All checkbox once
if ($('.select-all-container').length === 0) {
const $selectAllContainer = $(`
Select All
`);
$optionsContainer.prepend($selectAllContainer);
// Real fix: Stop mousedown before plugin closes the dropdown
$(document).on('mousedown', function (e) {
if ($(e.target).closest('.select-all-container').length) {
e.stopPropagation();
}
});
$('#select-all-checkbox').on('change', function () {
const isChecked = $(this).is(':checked');
$('#my-select option').prop('selected', isChecked);
$('#my-select').multiselect('reload');
});
}
// Add Apply/Cancel buttons once
if ($('.custom-button-wrapper').length === 0) {
const $wrapper = $('');
const $apply = $('Apply').on('click', function () {
const selected = $('#my-select').val();
alert('Selected: ' + (selected ? selected.join(', ') : 'None'));
});
const $cancel = $('Cancel').on('click', function () {
$('#my-select').val([]).multiselect('reload');
$('.ms-parent').find('.ms-drop').hide();
});
$wrapper.append($apply).append($cancel);
$optionsContainer.append($wrapper);
}
}
});
});< /code>
.custom-button {
display: inline-block;
margin: 10px 5px 5px;
padding: 5px 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.custom-button.cancel {
background-color: #dc3545;
}
.custom-button-wrapper {
text-align: center;
padding-bottom: 10px;
}
.select-all-container {
padding: 5px 10px;
border-bottom: 1px solid #ccc;
background: #f9f9f9;
user-select: none;
}
< /code>
MultiSelect with Fixed Select All
Select Options
Apple
Banana
Cherry
Date
Подробнее здесь:
https://stackoverflow.com/questions/796 ... h-checkbox
1747396528
Anonymous
Я пытаюсь достичь Multi Select Multi Select с флажками с настройкой кнопкой Apply и отменить кнопку внутри раскрывающегося списка. Когда я выбираю «Выбрать All < /code>», выпадает неожиданно закрывается, даже я пытался использовать StopPropagation < /code>, но все же он закрывает выпадающий список. Lang-HTML PrettyPrint-Override ">[code] MultiSelect with Fixed Select All Select Options Apple Banana Cherry Date < /code> < /div> < /div> < /p> $(document).ready(function () { $('#my-select').multiselect({ placeholder: 'Select fruits', onControlOpen: function () { const $optionsContainer = $('.ms-options'); // Add Select All checkbox once if ($('.select-all-container').length === 0) { const $selectAllContainer = $(` Select All `); $optionsContainer.prepend($selectAllContainer); // Real fix: Stop mousedown before plugin closes the dropdown $(document).on('mousedown', function (e) { if ($(e.target).closest('.select-all-container').length) { e.stopPropagation(); } }); $('#select-all-checkbox').on('change', function () { const isChecked = $(this).is(':checked'); $('#my-select option').prop('selected', isChecked); $('#my-select').multiselect('reload'); }); } // Add Apply/Cancel buttons once if ($('.custom-button-wrapper').length === 0) { const $wrapper = $(''); const $apply = $('Apply').on('click', function () { const selected = $('#my-select').val(); alert('Selected: ' + (selected ? selected.join(', ') : 'None')); }); const $cancel = $('Cancel').on('click', function () { $('#my-select').val([]).multiselect('reload'); $('.ms-parent').find('.ms-drop').hide(); }); $wrapper.append($apply).append($cancel); $optionsContainer.append($wrapper); } } }); });< /code> .custom-button { display: inline-block; margin: 10px 5px 5px; padding: 5px 10px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } .custom-button.cancel { background-color: #dc3545; } .custom-button-wrapper { text-align: center; padding-bottom: 10px; } .select-all-container { padding: 5px 10px; border-bottom: 1px solid #ccc; background: #f9f9f9; user-select: none; } < /code> MultiSelect with Fixed Select All Select Options Apple Banana Cherry Date [/code] Подробнее здесь: [url]https://stackoverflow.com/questions/79617240/jquery-multi-select-dropdown-with-checkbox[/url]