简体   繁体   English

如何使用Jquery更改Bootstrap下拉所选项目?

[英]How to change Bootstrap dropdown selected item with Jquery?

I want to change Bootstrap dropdown item select with jQuery? 我想更改使用jQuery选择的Bootstrap下拉项吗? I'm newbie in jQuery. 我是jQuery的新手。 I put dropdown list data from database with foreach loop. 我用foreach循环从数据库中放入下拉列表数据。 and I've a button. 我有一个按钮。 that have "id" and "value". 具有“ id”和“ value”的值。 When I click on that button, i want to change my bootstrap dropdown 'li' selected item to it button's "vaule" and "id".Please help me. 当我单击该按钮时,我想将我的引导程序下拉菜单“ li”所选项目更改为按钮的“ vaule”和“ id”。请帮助我。

<div class="dropdown" align="right">
    <input type="button" id="select_staff" class="btn btn-default btn-sm dropdown-toggle" type="button" data-toggle="dropdown" value="<?php echo $cur_admin_name ?> ▼">
    <ul id="admin_cal_list" class="dropdown-menu dropdown-menu-right">
    <input type="hidden" id="admin_id" class="form-control">
        <?php foreach ($admin_name as $admin_list): ?>
            <li id="<?php echo $admin_list["interview_plan_staff_id"]; ?>"><a href="#"><?php echo $admin_list["interview_plan_staff_name"]; ?></a></li>
        <?php endforeach; ?>
    </ul>
</div>

Like this.. 像这样..

$(".dropdown-menu li a").click(function(){
  var selText = $(this).text();
  console.log( selText);
  $(this).parents('.dropdown').find('.dropdown-toggle').val(selText);
});

Demo: http://www.bootply.com/EuFKxaQ2bO 演示: http//www.bootply.com/EuFKxaQ2bO

I'm not sure I 100% understand what you are trying to do. 我不确定我100%理解您要做什么。 For clarity, you want the li to change to the button's id and value? 为了清楚起见,您希望li更改为按钮的id和值吗? If so try this: 如果是这样,请尝试以下操作:

    $(".dropdown-menu li a").click(function(){
    document.getElementById("admin_cal_list").innerHTML = $(".dropdown-toggle").attr("id") + " " + $(".dropdown-toggle").attr("value");
});

Demo: http://www.bootply.com/UphW5kYtwE 演示: http//www.bootply.com/UphW5kYtwE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM