简体   繁体   English

如何使用jQuery将Bootstrap下拉菜单连接到事件处理程序?

[英]How do I hook up a Bootstrap dropdown to an eventhandler using jQuery?

Since a Bootstrap dropdown is actually a ul, I am confused on how to actually "hook it up" to functionality via jQuery. 由于Bootstrap下拉列表实际上是ul,因此我对如何通过jQuery实际上“挂钩”功能感到困惑。

For example, on the click of a submit button, I want something different to happen depending on which drop-down item is currently selected. 例如,单击提交按钮后,我希望根据当前选择的下拉项而有所不同。 See drop down code: 请参阅下拉代码:

<div class="dropdown">
    <button id='businessType' class="btn btn-default dropdown-toggle" data-toggle="dropdown">Business Type
        <span class="caret"></span></button>
    <ul class="dropdown-menu">
        <li><a href="#">Bar</a></li>
        <li><a href="#">Brewery</a></li>
        <li><a href="#">Food</a></li>
    </ul>
</div>

I want to do something like this: 我想做这样的事情:

var chosenType = $('businessType').val();

to get either Bar, Brewery, or Food and then use that chosenType variable to do something in my application but it's not working that way. 来获取Bar,Brewery或Food,然后使用该selectedType变量在我的应用程序中执行某些操作,但这种方式无法正常工作。 I cannot seem to find this in the docs or any previous question which addresses this. 我似乎无法在文档或任何以前的解决此问题的问题中找到此问题。

Need to use jQuery selector for single selection : 需要使用jQuery选择器进行单选:

$($('#businessType').parent().find('ul.dropdown-menu li a')[0]).text()

for all selection : 对于所有选择:

   $.each($('#businessType').parent().find('ul.dropdown-menu li a'),function(i,v){ console.log($(v).text());
    })

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

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