简体   繁体   English

如何在表单中的多个选择类型下拉菜单中使用jQuery脚本?

[英]How to use jQuery script for multiple select type dropdown menus in a form?

I have three dropdown menus which I want to display the selected item on the button for each. 我有三个下拉菜单,我要在每个菜单上显示所选的项目。 However, my jQuery code is changing the selected item for all three dropdowns. 但是,我的jQuery代码正在更改所有三个下拉菜单的选定项。 If I click on one item in any of the dropdowns, all three change to the item I've selected. 如果我在任何一个下拉菜单中单击一个项目,则所有三个项目都将更改为我选择的项目。 How do I make it so they display their own selected items separately? 如何使它们分别显示自己选择的项目?

HTML HTML

           <div class="form-inline">
                <div class="form-group">
                    <button type="button" id="vegetype" name="vegetype" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All vege types <i class="fa fa-angle-down" aria-hidden="true"></i></button>
                    <ul class="dropdown-menu">
                        <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">All</label></li>
                        <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Brocolli</label></li>
                        <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Cucumber</label></li>
                        <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Kale</label></li>
                        <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Carrot</label></li>
                    </ul>
                </div><!-- end form-group -->

                <div class="form-group">
                    <button type="button" id="fruits" name="fruits" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Fruits <i class="fa fa-angle-down" aria-hidden="true"></i></button>
                    <ul class="dropdown-menu">
                        <li class="dropdown-item">Apple</li>
                        <li class="dropdown-item">Orange</li>
                        <li class="dropdown-item">Banana</li>
                    </ul>
                </div><!-- end form-group -->

                <div class="form-group">
                    <button type="button" id="max_num" name="max_num" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Number <i class="fa fa-angle-down" aria-hidden="true"></i></button>
                    <ul class="dropdown-menu">
                        <li class="dropdown-item">1</li>
                        <li class="dropdown-item">2</li>
                        <li class="dropdown-item">3</li>
                        <li class="dropdown-item">4</li>
                    </ul>
                </div><!-- end form-group -->
            </div>

JS JS

$(document).ready(function(){
  $(".dropdown-menu li").click(function(){
    $(".btn").text($(this).text());
  });

});

Change $(".btn").text($(this).text()); 更改$(".btn").text($(this).text()); To $(this).parent().siblings('.btn').text($(this).text()); $(this).parent().siblings('.btn').text($(this).text());

 $(document).ready(function(){ $(".dropdown-menu li").click(function(){ $(this).parent().siblings('.btn').text($(this).text()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-inline"> <div class="form-group"> <button type="button" id="vegetype" name="vegetype" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All vege types <i class="fa fa-angle-down" aria-hidden="true"></i></button> <ul class="dropdown-menu"> <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">All</label></li> <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Brocolli</label></li> <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Cucumber</label></li> <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Kale</label></li> <li class="dropdown-item"><label class="form-check-label"><input class="form-check-input" type="checkbox" value="">Carrot</label></li> </ul> </div><!-- end form-group --> <div class="form-group"> <button type="button" id="fruits" name="fruits" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Fruits <i class="fa fa-angle-down" aria-hidden="true"></i></button> <ul class="dropdown-menu"> <li class="dropdown-item">Apple</li> <li class="dropdown-item">Orange</li> <li class="dropdown-item">Banana</li> </ul> </div><!-- end form-group --> <div class="form-group"> <button type="button" id="max_num" name="max_num" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Number <i class="fa fa-angle-down" aria-hidden="true"></i></button> <ul class="dropdown-menu"> <li class="dropdown-item">1</li> <li class="dropdown-item">2</li> <li class="dropdown-item">3</li> <li class="dropdown-item">4</li> </ul> </div><!-- end form-group --> </div> 

Change this: 更改此:

$(".btn").text($(this).text());

to

$(this).closest(".btn").text($(this).text());

and try again. 然后再试一次。

Explanation: When you are using $(".btn").text($(this).text()); 说明:当您使用$(".btn").text($(this).text()); , means you are selecting all the buttons, but you have to select only the associated button itself, so yiu have to use DOM traversing function to get the right button. ,表示您正在选择所有按钮,但是您只需要选择关联的按钮本身,因此yiu必须使用DOM遍历功能来获取正确的按钮。

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

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