[英]Bootstrap4 dropdown with button - get the selected element
I have a button with dropdown in Bootstrap4. 我在Bootstrap4中有一个带有下拉菜单的按钮。 The HTML looks as following:
HTML如下所示:
<div class="row" id="dropdown-box">
<div class="col-lg-6">
<div class="input-group">
<div class="input-group-btn" id="button-group-id">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select
</button>
<div class="dropdown-menu">
{% for skill in skills %}
<a class="dropdown-item" href="#">{{skill.name}}</a>
{% endfor %}
</div>
</div>
<input type="text" class="form-control" aria-label="Text input with dropdown button">
</div>
</div>
Here's the Javascript, that has no effect :( 这是无效的Javascript :(
<script>
$( document ).ready(function() {
$("#button-group-id").on("show.bs.dropdown", function(event){
var x = $(event.relatedTarget).text(); // Get the text of the element
console.log(x);
});
});
</script>
Try with this js 试试这个js
$( document ).ready(function() {
$(".dropdown-menu a").click(function(){
var selText = $(this).text();
console.log(selText);
});
});
fiddle : https://jsfiddle.net/5rpwvzsw/ 小提琴: https : //jsfiddle.net/5rpwvzsw/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.