简体   繁体   English

如何从Bootstrap 4下拉项目菜单显示所选项目

[英]How display selected item from Bootstrap 4 drop-down item menu

I'm trying to display what the user selects once they click on an item from the Boostrap 4 drop down menu. 我试图显示用户单击Boostrap 4下拉菜单中的项目后所选择的内容。

Ex. 例如 "What is your eye color" User selects : Blue - Blue is displayed after click. 用户选择“您的眼睛是什么颜色”:蓝色-单击后显示蓝色。

Ex. 例如 "What is your skin tone" User selects: Fair- Fair is displayed after click. 用户选择“您的肤色是什么”:Fair-单击后显示Fair。

I have done research and jQuery seems to be the best bet however all of the solutions are for older bootstrap versions. 我已经进行了研究,jQuery似乎是最好的选择,但是所有解决方案都适用于较旧的引导程序版本。 I'm using Boostrap 4 and there are no li tags in the bootstrap 4 code, it's all a class tags so I'm not sure how to fit that into the jQuery option below. 我正在使用Boostrap 4,而bootstrap 4代码中没有li标记,因为它们都是类标记,所以我不确定如何将其放入下面的jQuery选项。 Also, I have (2) dropdown menus - please see pic. 另外,我有(2)下拉菜单-请参见图片。 I'm not sure if this makes it more tricky. 我不确定这是否会更棘手。 Any assistance or direction would be great. 任何帮助或指导都会很棒。

jQuery: jQuery的:

$(document).ready (function(){

$('#selectmenu1 a').click(function){

}

HTML: HTML:

 <!--Drop down Item 1 -->
<h3 class="display-4" style="font-size: 1.5rem;">What is your eye color</h3>
<div class="dropdown">
  <button class="btn btn-info btn-md dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style="background-color: #588c7e;">
    Eye Color
  </button>


  <div class="dropdown-menu" onchange="selectMenu1" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#" data="brown"><img src="img/brown_eye.jpg" class="rounded-circle"> Brown</a>
    <a class="dropdown-item" href="#" data="blue"><img src="img/blue_eye.jpg" class="rounded-circle" > Blue</a>
    <a class="dropdown-item" href="#" data="green"><img src="img/green_eye.jpg" class="rounded-circle" > Green</a>
    <a class="dropdown-item" href="#" data="hazel"><img src="img/hazel_eye.jpg" class="rounded-circle" > Hazel</a>
  </div>

</div>


<!--Drop down Item 2-->
<h3 class="display-4" style="font-size: 1.5rem;"> What is your skin tone</h3>
<div id="menu2" class="dropdown">
  <button class="btn btn-info btn-md dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style="background-color: #588c7e;">
    Skin Tone
  </button>

  <div class="dropdown-menu" onchange="selectMenu2" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#" data="fair"><img src="img/fair.jpg" class="rounded-circle" > Fair (porcelain)</a>
    <a class="dropdown-item" href="#" data="light"><img src="img/light.jpg" class="rounded-circle" > Light (fair to light)</a>
    <a class="dropdown-item" href="#" data="medium"><img src="img/medium.jpg" class="rounded-circle" > Medium (light to medium)</a>
    <a class="dropdown-item" href="#" data="bronze"><img src="img/bronze_dark.jpg" class="rounded-circle" > Bronze dark (deep tan)</a>
    <a class="dropdown-item" href="#" data="tan"><img src="img/tan.jpg" class="rounded-circle" > Tan (warm to medium)</a>
    <a class="dropdown-item" href="#" data="rich"><img src="img/dark.jpg" class="rounded-circle" > Rich (deep)</a>
  </div>

</div>

<script>

    document.onreadystatechange = function () {
      if (document.readyState === "interactive") {
          initApplication();
      }
  }


  var eyeColor = null;
  function selectMenu1(value){
    eyeColor = value;
  }

  var skinTone = null;
  function selectMenu2(value){
    skinTone = value;
  }

  function validate()  { 
    if (eyeColor && skinTone){
      // alert(`You selected ${eyeColor} eye colour and ${skinTone} skin tone.`);

    if (eyeColor=="brown" && skinTone=="fair"){
      window.location = "brown/brown_fair.html";
    } else if (eyeColor=="brown" && skinTone=="light"){
      window.location = "brown/brown_light.html";
    } else if (eyeColor=="brown" && skinTone=="medium"){
      window.location = "brown/brown_medium.html";
    } else if (eyeColor=="brown" && skinTone=="bronze"){
      window.location = "brown/brown_bronze.html";
    } else if (eyeColor=="brown" && skinTone=="tan"){
      window.location = "brown/brown_tan.html";
    } else if (eyeColor=="brown" && skinTone=="rich"){
      window.location = "brown/brown_rich.html";
    }   

    if (eyeColor=="blue" && skinTone=="fair"){
      window.location = "blue/blue_fair.html";
    } else if (eyeColor =="blue" && skinTone=="light"){
      window.location = "blue/blue_light.html";
    } else if (eyeColor =="blue" && skinTone=="medium"){
      window.location = "blue/blue_medium.html";
    } else if (eyeColor =="blue" && skinTone=="bronze"){
      window.location = "blue/blue_bronze.html";
    } else if (eyeColor=="blue" && skinTone=="tan"){
      window.location = "blue/blue_tan.html";
    } else if (eyeColor=="blue" && skinTone=="rich"){
      window.location = "blue/blue_rich.html";
    }

    if (eyeColor=="green" && skinTone=="fair"){
        window.location = "green/green_fair.html";
    } else if (eyeColor == "green" && skinTone=="light"){
      window.location= "green/green_light.html";
    } else if (eyeColor== "green" && skinTone=="medium"){
      window.location="green/green_medium.html";
    } else if (eyeColor=="green" && skinTone=="bronze"){
      window.location="green/green_bronze.html";
    } else if (eyeColor=="green" && skinTone=="tan"){
      window.location="green/green_tan.html";
    } else if (eyeColor=="green" && skinTone=="rich"){
      window.location="green/green_rich.html";
    }


    if (eyeColor=="hazel" && skinTone=="fair"){
        window.location = "hazel/hazel_fair.html";
    } else if (eyeColor=="hazel" && skinTone=="light"){
      window.location="hazel/hazel_light.html";
    } else if (eyeColor=="hazel" && skinTone=="medium"){
      window.location="hazel/hazel_medium.html";
    } else if (eyeColor=="hazel" && skinTone=="bronze"){
      window.location="hazel/hazel_bronze.html";
    } else if (eyeColor=="hazel" && skinTone=="tan"){
      window.location="hazel/hazel_tan.html";
    } else if (eyeColor=="hazel" && skinTone=="rich"){
      window.location="hazel/hazel_rich.html";
    }
  }

    else if (!eyeColor){
      alert("Please pick an eye colour");
    } else if (!skinTone){
      alert("Please pick a skin tone");
    }
    }

  function initApplication(){
    //setup dropdown menu selection events
    Array.from(document.querySelectorAll(".dropdown-menu")).forEach((menu,idx)=>{
      const menuCallbackName = menu.attributes.onchange.value;
      const fetchedCallback = window[menuCallbackName] || null;

      if (fetchedCallback){
        Array.from(menu.children).forEach((child)=>{
          const callbackValue = child.attributes.data ? child.attributes.data.value : null;
          if (callbackValue) child.onclick = () => fetchedCallback(callbackValue);
        });
      } else console.error(`No callback function named ${menuCallbackName} for menu ${idx}`);

    });
  }
</script>

参考照片

You can access the data using jQuery's "attr()" function. 您可以使用jQuery的“ attr()”函数访问数据。

$('#selectmenu1 a').click(
  function( event ){
    // Prevents browser from treating like normal anchor tag
    event.preventDefault()

    // Retrieves data in anchor tag
    let data = $(this).attr('data')
  }
)

Side note, it is possible to use list tags for this in Bootstrap. 旁注, 可以在Bootstrap中为此使用列表标签。 Bootstrap is really flexible and robust. Bootstrap确实非常灵活和强大。 Also, in my opinion, it would be most semantically correct to use "select". 而且,在我看来,使用“选择”在语义上最正确。

I found the solution: 我找到了解决方案:

I included input-group into my html, as well as included jQuery: 我将input-group包含到我的html中,并包含了jQuery:

HTML: HTML:

    <h3 class="display-4" style="font-size: 1.5rem;">What is your eye color</h3>
    <div class="dropdown">
        <div class="input-group justify-content-center">
            <div class="input-group-btn">
                <button class="btn btn-info btn-md dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style="background-color: #588c7e;">
                    Eye Color

                </button>

                <div class="dropdown-menu" onchange="selectMenu1" aria-labelledby="dropdownMenuButton">
                    <a class="dropdown-item" href="#" data="brown" ><img src="img/brown_eye.jpg" class="rounded-circle"> Brown</a>
                    <a class="dropdown-item" href="#" data="blue" ><img src="img/blue_eye.jpg" class="rounded-circle" > Blue</a>
                    <a class="dropdown-item" href="#" data="green" ><img src="img/green_eye.jpg" class="rounded-circle" > Green</a>
                    <a class="dropdown-item" href="#" data="hazel" ><img src="img/hazel_eye.jpg" class="rounded-circle" > Hazel</a>
                </div>

            </div>
        </div>
    </div>



<script>
    $(".dropdown-menu a ").click(function(){
                $(this).parents(".input-group-btn").find('.btn').text($(this).text());
            });
</script>

And if even easier 而且甚至更容易

<script>
$(".dropdown-menu a ").click(function () {
    let x = $(this).text();
    alert(x);
});
</script>

Works with this HTML below. 使用下面的HTML。 No input-group needed and jQuery short enough. 不需要输入组,jQuery也足够短。

<nav class="navbar navbar-light bg-light">
  <a class="navbar-brand" data-toggle="modal" href="#myModal">Navbar</a>
  <div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" type="button" 
id="dropdownMenuButton" style="width:4rem"
            data-toggle="dropdown">en
    </button>
    <div class="dropdown-menu" id="dropDownMenu" aria- 
    labelledby="dropdownMenuButton">
      <a class="dropdown-item" href="#">en</a>
      <a class="dropdown-item" href="#">de</a>
    </div>
  </div>
</nav>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM