[英]JQuery to get value from bootstrap dropdown menu
Following is my html code which came through handlebars 以下是我的html代码,它来自车把
<div class="form-group" id="lab-domain-body"> <label or="scheduleConfigurationFieldDropdown">Select Configuration</label> <div class="dropdown form-control--transparent"> <input type="TextBox" value id="configuration" class="schedule-configuration sr-only" readonly required/> <button class="btn btn__dropdown dropdown-toggle" type="button" id="scheduleConfigurationFieldDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="test1">v1.3</button> <ul class="dropdown-menu modal-body__schedule-ul" id="demolist" aria-labelledby="scheduleConfigurationFieldDropdown"> <li><span> v1.3</span></li> <li><span>MDSO</span></li> <li><span>v1.6</span></li> <li><span>v11.8</span></li> </ul> </div> </div>
I want as soon user selects item just need to get value from dropdown menu. 我希望尽快用户选择项目只需要从下拉菜单中获取值。 Following is a Jquery which i tried till now, tried everything but nothing seems to work in my case.
以下是我现在尝试过的一个Jquery,尝试了一切,但似乎没有什么工作在我的情况下。
//This function is never get called $('#demolist li a').on('click', function(){ $('#configuration').val($(this).text()); });
I tried enable submit button which is working fine. 我尝试启用提交按钮,工作正常。 But still not able to get value from dropdown menu.
但仍无法从下拉菜单中获取价值。 I didn't think it should be hard enough but stuck.
我不认为它应该足够难,但卡住了。 Tried every method on stackoverflows.
尝试了stackoverflows上的每个方法。
$form.on('change input', 'input', enableSubmit); function enableSubmit(xyz) { console.log(($('.schedule-configuration')).val()); var bc = $('#configuration').val($(this).text()); var xyz = ($('.schedule-configuration')).text(); console.log(xyz[0]); // Jquery to get value from selected $formSubmit.prop('disabled', false); }
You're attaching the click event handler to $('#demolist li a')
, but there's no <a>
element in your <ul>
. 您将click事件处理程序附加到
$('#demolist li a')
,但<ul>
没有<a>
元素。 Try attaching the event handler to $('#demolist li span')
. 尝试将事件处理程序附加到
$('#demolist li span')
。
EDIT: 编辑:
You need to wrap the attached event in a $(document).ready() function. 您需要将附加事件包装在$(document).ready()函数中。
Here's a working fiddle: https://jsfiddle.net/hc4rpyt8/8/ 这是一个工作小提琴: https : //jsfiddle.net/hc4rpyt8/8/
And here's the code you need: 这是您需要的代码:
$( document ).ready(function() {
$('#demolist li span').on('click', function(){
$('#configuration').val($(this).text());
});
});
Working code: 工作代码:
$(document).on('click', '#demolist li', function() {
$('#configuration').val($(this).children('span').text());
});
And a fiddle here: https://jsfiddle.net/beekvang/xj5oponw/ 这里有一个小提琴: https : //jsfiddle.net/beekvang/xj5oponw/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.