[英]Get selected text from bootstrap dropdown
I have a bootstrap dropdown, and I want to keep the text of what is selected.我有一个引导程序下拉菜单,我想保留所选内容的文本。 I have tried most of the ways tried here, Get Value From html drop down menu in jquery , but there was never really an answer given, so I am still stuck.
我已经尝试了这里尝试的大多数方法, 从 jquery 中的 html 下拉菜单中获取价值,但从来没有真正给出答案,所以我仍然被卡住了。
<div id="tableDiv" class="dropdown">
<button id="tableButton" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
Table
<span class="caret"></span>
</button>
<ul id="tableMenu" class="dropdown-menu">
<li><a href="#">Att1</a></li>
<li><a href="#">Att2</a></li>
<li><a href="#">Att3</a></li>
</ul>
</div>
I have a js function that looks as follows我有一个如下所示的 js 函数
$("#tableMenu").click(function(){
var selText = $("#tableMenu").text();
document.getElementById("tableButton").innerHTML=selText;
});
but as explained in the above link, $("#tableMenu").text();但正如上面链接中所解释的,$("#tableMenu").text(); returns everything in the dropdown as a string, not just the one wanted.
将下拉列表中的所有内容作为字符串返回,而不仅仅是想要的。 How do I get the text just from the one selected.
如何仅从选定的文本中获取文本。
You need to attach the click()
handler to the a
element within the ul
that gets toggled.您需要将
click()
处理程序附加到被切换的ul
中的a
元素。 You can then retrieve the text()
of that element and set it to the related button.然后,您可以检索该元素的
text()
并将其设置为相关按钮。 Try this:尝试这个:
$("#tableMenu a").on('click', function(e) { e.preventDefault(); // cancel the link behaviour var selText = $(this).text(); $("#tableButton").text(selText); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="tableDiv" class="dropdown"> <button id="tableButton" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown"> Table <span class="caret"></span> </button> <ul id="tableMenu" class="dropdown-menu"> <li><a href="#">Att1</a></li> <li><a href="#">Att2</a></li> <li><a href="#">Att3</a></li> </ul> </div>
Have you tried using an actual HTML drop down box and not an unordered list?您是否尝试过使用实际的 HTML 下拉框而不是无序列表?
<select name="tableMenu" id="tableMenu" class="dropdown-menu">
<option value="Att1">Att1</option>
<option value="Att2">Att2</option>
<option value="Att3">Att3</option>
</select>
You should then be able to use:然后,您应该能够使用:
var selText= document.getElementById("tableMenu").value;
Bind the click event to li
and using this
get the selected text and assign to the tab button text将单击事件绑定到
li
并使用this
获取所选文本并分配给选项卡按钮文本
$("#tableMenu li").click(function () {
var selText = $(this).text();
document.getElementById("tableButton").innerHTML = selText; // $("#tableButton").text(selText); //Using Jquery
});
Here's a vanillaJS way:这是一个 vanillaJS 的方式:
Given the following bootstrap 4 dropdown snippet:鉴于以下引导程序 4 下拉片段:
<div class="dropdown-menu" id="select-color-dropdown" aria-labelledby="select-color">
<a class="dropdown-item" href="#">Red</a>
<a class="dropdown-item" href="#">Orange</a>
<a class="dropdown-item" href="#">Green</a>
<a class="dropdown-item" href="#">Gray</a>
</div>
One could write an event listener:可以编写一个事件侦听器:
/* find web elements */
const colorSelectors = document.querySelector("#select-color-dropdown");
/* define event listeners */
const loadEventListeners = () => {
colorSelectors.addEventListener("click", (e) => {
e.preventDefault();
console.log(e.target.innerText);
});
}
/* Load all event listeners */
loadEventListeners();
which would print the selected option on every select (Red, Green...)它将在每个选择(红色,绿色...)上打印选定的选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.