[英]Bootstrap 5 Dropdowns: Get selected dropdown item value in the dropdown button
I have a simple issue with my dropdown button while I'm trying to get the selected dropdown item to the dropdown button value.当我尝试将选定的下拉项目设置为下拉按钮值时,我的下拉按钮有一个简单的问题。
The Flag Icon and Text should be changed dynamically I made some examples but as I see it's not working as I expected.标志图标和文本应该动态更改我做了一些例子,但我看到它没有按我预期的那样工作。
The only issue that I have is the flag icon not changed dynamically我唯一的问题是旗帜图标没有动态改变
Live Example: https://codepen.io/themes4all/pen/wvdemKz现场示例: https : //codepen.io/themes4all/pen/wvdemKz
HTML: HTML:
<div class="dropdown">
<button class="btn btn-primary btn-lg dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li>
<a class="dropdown-item active" href="#"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></a>
</li>
<li>
<a class="dropdown-item" href="#"><span class="flag-icon flag-icon-fr me-1"></span> <span>French</span></a>
</li>
<li>
<a class="dropdown-item" href="#"><span class="flag-icon flag-icon-es me-1"></span> <span>Spanich</span></a>
</li>
<li>
<a class="dropdown-item" href="#"><span class="flag-icon flag-icon-sa me-1"></span> <span>Arabic</span></a>
</li>
</ul>
</div>
jQuery jQuery
if ($(".dropdown").length) {
$(document).on("click", ".dropdown-menu .dropdown-item", function (e) {
e.preventDefault();
if (!$(this).hasClass("active")) {
var swalWithBootstrapButtons = Swal.mixin({
customClass: {
confirmButton: "btn btn-primary",
cancelButton: "btn btn-danger me-3",
},
buttonsStyling: false,
});
swalWithBootstrapButtons
.fire({
title: "Are you sure?",
text: "Do you really want to change your current language!",
icon: "warning",
confirmButtonText: "<i class='fas fa-check-circle me-1'></i> Yes, I am!",
cancelButtonText: "<i class='fas fa-times-circle me-1'></i> No, I'm Not",
showCancelButton: true,
reverseButtons: true,
focusConfirm: true,
})
.then((result) => {
if (result.isConfirmed) {
if (!$(this).hasClass("active")) {
$(".dropdown-menu .dropdown-item").removeClass("active");
$(this).addClass("active");
$(this)
.parents(".dropdown")
.find(".btn")
.html("<span class='flag-icon flag-icon-us me-1'></span>" + $(this).text());
}
Swal.fire({
icon: "success",
title: "Amazing!",
text: "Your current language has been changed successfully.",
showConfirmButton: false,
timer: 1500,
});
}
});
}
});
}
you need to replace .html("<span class='flag-icon flag-icon-us me-1'></span>" + $(this).text());
你需要替换.html("<span class='flag-icon flag-icon-us me-1'></span>" + $(this).text());
with .html($(this).html());
与.html($(this).html());
, it will give you the desired output ,它将为您提供所需的输出
Please check this, I have just updated the code to get class of clicked option and added to the span in HTML请检查这个,我刚刚更新了代码以获取单击选项的类并添加到 HTML 中的跨度
if ($(".dropdown").length) { $(document).on("click", ".dropdown-menu .dropdown-item", function (e) { e.preventDefault(); var cloneflagicon = $(this).find('span.flag-icon').attr('class'); if (!$(this).hasClass("active")) { var swalWithBootstrapButtons = Swal.mixin({ customClass: { confirmButton: "btn btn-primary", cancelButton: "btn btn-danger me-3", }, buttonsStyling: false, }); swalWithBootstrapButtons .fire({ title: "Are you sure?", text: "Do you really want to change your current language!", icon: "warning", confirmButtonText: "<i class='fas fa-check-circle me-1'></i> Yes, I am!", cancelButtonText: "<i class='fas fa-times-circle me-1'></i> No, I'm Not", showCancelButton: true, reverseButtons: true, focusConfirm: true, }) .then((result) => { if (result.isConfirmed) { if (!$(this).hasClass("active")) { $(".dropdown-menu .dropdown-item").removeClass("active"); $(this).addClass("active"); $(this) .parents(".dropdown") .find(".btn") .html("<span class='"+cloneflagicon+"'></span>" + $(this).text()); } Swal.fire({ icon: "success", title: "Amazing!", text: "Your current language has been changed successfully.", showConfirmButton: false, timer: 1500, }); } }); } }); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap 5</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css" rel="stylesheet" /> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" /> <link href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.5.0/css/flag-icon.min.css" rel="stylesheet" /> </head> <body> <div class="dropdown"> <button class="btn btn-primary btn-lg dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></button> <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1"> <li> <a class="dropdown-item active" href="#"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></a> </li> <li> <a class="dropdown-item" href="#"><span class="flag-icon flag-icon-fr me-1"></span> <span>French</span></a> </li> <li> <a class="dropdown-item" href="#"><span class="flag-icon flag-icon-es me-1"></span> <span>Spanich</span></a> </li> <li> <a class="dropdown-item" href="#"><span class="flag-icon flag-icon-sa me-1"></span> <span>Arabic</span></a> </li> </ul> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.