JQuery从bootstrap下拉菜单中获取价值

时间:2017-05-12 13:21:11

标签: javascript jquery html

以下是我的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>
&#13;
&#13;
&#13;

我希望尽快用户选择项目只需要从下拉菜单中获取值。 以下是我现在尝试过的一个Jquery,尝试了一切,但似乎没有什么工作在我的情况下。

  1. //This function is never get called  
    
    $('#demolist li a').on('click', function(){
        $('#configuration').val($(this).text());
    });
    
  2. 我尝试启用提交按钮,工作正常。但仍无法从下拉菜单中获取价值。我并不认为它应该足够硬但是卡住了。尝试了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);
    }
    
  3. enter image description here

2 个答案:

答案 0 :(得分:0)

您已将点击事件处理程序附加到$('#demolist li a'),但<a>中没有<ul>元素。尝试将事件处理程序附加到$('#demolist li span')

编辑:

您需要将附加事件包装在$(document).ready()函数中。

这是一个工作小提琴:https://jsfiddle.net/hc4rpyt8/8/

以下是您需要的代码:

$( document ).ready(function() {
    $('#demolist li span').on('click', function(){
        $('#configuration').val($(this).text());
    });
});

答案 1 :(得分:0)

工作代码:

$(document).on('click', '#demolist li', function() {
  $('#configuration').val($(this).children('span').text());
});

这里有一个小提琴:https://jsfiddle.net/beekvang/xj5oponw/

相关问题