简体   繁体   English

如何从Bootstrap 4的下拉菜单中获取价值?

[英]How to get value from dropdown menu in bootstrap 4?

<li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle toggle2" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Material Type
                </a>


                <div class="dropdown-menu menu2" aria-labelledby="navbarDropdown2">
                    <a class="dropdown-item">Mud</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item">Cloth</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item">Thread</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item">Jute</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item">Cotton</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item">Cane</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item">Bamboo</a>
                </div>
            </li>

Can I get the value from the menu after selecting like following? 选择如下所示后,能否从菜单中获取值?

var email=document.getElementById('signUpEmail').value;

So that I can use this variable in ajax. 这样我就可以在ajax中使用此变量。

Edit 编辑
As @Timir suggest to use select then the menu is look like 正如@Timir建议使用select一样,菜单看起来像

<li class="nav-item">
<select class="custom-select custom-select-lg mb-3" id="mydropdown">
  <option selected>Material Type</option>
  <option value="Mud">Mud</option>
  <option value="Cloth">Cloth</option>
  <option value="Thread">Thread</option>
  <option value="Jute">Jute</option>
  <option value="Cotton">Cotton</option>
  <option value="Can">Can</option>
  <option value="Bamboo">Bamboo</option>
</select>
</li>

Can I get the value from the menu after selecting like following? 选择如下所示后,能否从菜单中获取值?

No, there are no input to retrieve their value. 不,没有输入可检索其值。 but you can get the text inside the link like 但您可以在链接内获得文本,例如

$('.menu2 a.dropdown-item').on('click', function(){
    $(this).text()
});

把它变成一个选择,给它一个ID,然后说: $(“#mydropdown option:selected”).val();

There are workarounds for basically everything in JavaScript. 基本上所有JavaScript都有解决方法。 Including this. 包括这个。 Without using JQuery, you could add an event listener, or you could make an onlick within each option or anchor tag. 在不使用JQuery的情况下,您可以添加事件侦听器,也可以在每个选项或锚标记中添加一个onlick。 When you click on the drop down and set the option, it calls the function which sets the variable. 当您单击下拉菜单并设置选项时,它将调用设置变量的函数。 So even though there is no input box... you are just setting a global variable. 因此,即使没有输入框,您也只是在设置全局变量。 And then you utilize that variable as a part of a form or however you plan on using it. 然后,您可以将该变量用作表单的一部分,或者计划使用它。

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

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