【问题标题】:jQuery functions, one to display dropdown on click and another to get selected item from dropdownjQuery 函数,一个用于在单击时显示下拉菜单,另一个用于从下拉列表中获取所选项目
【发布时间】:2014-10-09 14:33:35
【问题描述】:

我有一个无序列表(下面是 HTML,下面是 Javascript 代码)。

第一个函数“display_dropdown_multiple”对我有用。但是,我遇到了问题 第二个函数“get_selected_owner”。

从下拉菜单中进行选择后。我希望第二个函数返回“选定值”。任何帮助将不胜感激。

function display_dropdown_multiple(event) {
    $(".dropdown-menu").on('click', 'li a', function(){
      $(this).parents(".btn-group").find('.btn').text($(this).text());
      $(this).parents(".btn-group").find('.btn').val($(this).data('value'));
       });
});

function get_selected_owner() {
    return $('#owner').find('option:selected').val();
    });        
});
<div class="btn-group">
    <button type="button" class="btn btn-success dropdown-toggle" id="owner" data-toggle="dropdown">
      Owner <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="owner-dropdown-menu">
      <li role="presentation" class="dropdown-header">major</li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#" id="owner"><input type="hidden" onclick='display_dropdown_multiple(event)' id="ownerId" name="ownerId" value="Owner 1" >Owner 1</a></li>
      <li class="divider"></li>
      <li role="presentation" class="dropdown-header">minor</li>
      <li><a href="#" id="owner"><input type="hidden" onclick='display_dropdown_multiple(event)' id="ownerId" name="ownerId" value="Owner 2" >Owner 2</a></li>
      <li><a href="#" id="owner"><input type="hidden" onclick='display_dropdown_multiple(event)' id="ownerId" name="ownerId" value="Owner 3" >Owner 3</a></li>
    </ul>
</div>

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap


    【解决方案1】:
    1. ID 必须是唯一的
    2. 没有您要定位的&lt;option&gt;

    解决方案

    构造一个 &lt;select&gt; ,其中包含您打算与唯一 ID 一起使用的选项,然后您的代码就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2016-02-21
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多