简体   繁体   English

如何在html中显示下拉选择按钮?

[英]How to show drop down select in button in html?

Here I am listing hour and minute in two different buttons. 在这里,我用两个不同的按钮列出小时和分钟。 I can able to drop down hour and minute, but not showed selected value in button. 我可以下拉小时和分钟,但不能在按钮中显示选定的值。

        <div class="btn-group btn-group-sm" role="group">
          <button id="startHour" type="button" class="btn btn-secondary" data-toggle="dropdown" href="#">hour</button>

            <div class="dropdown-menu" aria-labelledby="startHour">
              <a class="dropdown-item" href="#">1</a>
              <a class="dropdown-item" href="#">2</a>
              <a class="dropdown-item" href="#">3</a>
              <a class="dropdown-item" href="#">4</a>
              <a class="dropdown-item" href="#">5</a>
              <a class="dropdown-item" href="#">6</a>
              <a class="dropdown-item" href="#">7</a>
              <a class="dropdown-item" href="#">8</a>
              <a class="dropdown-item" href="#">9</a>
              <a class="dropdown-item" href="#">10</a>
              <a class="dropdown-item" href="#">11</a>
              <a class="dropdown-item" href="#">12</a>
            </div>
            <span style="color:white; background-color:#5A6268">:</span>
<div class="btn-group btn-group-sm" role="group">
          <button id="startMinute" type="button" class="btn btn-secondary" 
data-toggle="dropdown" href="#">min</button>
          <div class="dropdown-menu" aria-labelledby="startMinute">
            <a class="dropdown-item" href="#">00</a>
            <a class="dropdown-item" href="#">05</a>
            <a class="dropdown-item" href="#">10</a>
            <a class="dropdown-item" href="#">15</a>
            <a class="dropdown-item" href="#">20</a>
            <a class="dropdown-item" href="#">25</a>
            <a class="dropdown-item" href="#">30</a>
            <a class="dropdown-item" href="#">35</a>
            <a class="dropdown-item" href="#">40</a>
            <a class="dropdown-item" href="#">45</a>
            <a class="dropdown-item" href="#">50</a>
            <a class="dropdown-item" href="#">55</a>
          </div>
          <button type="button" class="btn btn-secondary">am</button>
          <button type="button" class="btn btn-secondary">zone</button>
          <button type="button" class="btn btn-primary">Set</button>

      </div>

And javascript I used 和我用过的javascript

  $(".dropdown-menu a").click(function(){
    var selText = $(this).text();
  $(this).parents('.btn-group').find('.btn-secondary').html(selText);
   });

By using above js code I can able to display selected value in button, but it changes value to all drop down list. 通过使用上面的js代码,我可以在按钮中显示选定的值,但是它将值更改为所有下拉列表。 Here is the screenshots which i am getting 这是我得到的屏幕截图 屏幕截图

You forgot to close the divs 您忘记关闭div

 $(".dropdown-menu a").click(function() { var selText = $(this).text(); $(this).parents('.btn-group').find('.btn-secondary').html(selText); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script> <div class="btn-group btn-group-sm" role="group"> <button id="startHour" type="button" class="btn btn-secondary" data-toggle="dropdown" href="#">hour</button> <div class="dropdown-menu" aria-labelledby="startHour"> <a class="dropdown-item" href="#">1</a> <a class="dropdown-item" href="#">2</a> <a class="dropdown-item" href="#">3</a> <a class="dropdown-item" href="#">4</a> <a class="dropdown-item" href="#">5</a> <a class="dropdown-item" href="#">6</a> <a class="dropdown-item" href="#">7</a> <a class="dropdown-item" href="#">8</a> <a class="dropdown-item" href="#">9</a> <a class="dropdown-item" href="#">10</a> <a class="dropdown-item" href="#">11</a> <a class="dropdown-item" href="#">12</a> </div> </div> <span style="color:white; background-color:#5A6268">:</span> <div class="btn-group btn-group-sm" role="group"> <button id="startMinute" type="button" class="btn btn-secondary" data-toggle="dropdown" href="#">min</button> <div class="dropdown-menu" aria-labelledby="startMinute"> <a class="dropdown-item" href="#">00</a> <a class="dropdown-item" href="#">05</a> <a class="dropdown-item" href="#">10</a> <a class="dropdown-item" href="#">15</a> <a class="dropdown-item" href="#">20</a> <a class="dropdown-item" href="#">25</a> <a class="dropdown-item" href="#">30</a> <a class="dropdown-item" href="#">35</a> <a class="dropdown-item" href="#">40</a> <a class="dropdown-item" href="#">45</a> <a class="dropdown-item" href="#">50</a> <a class="dropdown-item" href="#">55</a> </div> </div> 

it changing value for all – PvDev 改变所有人的价值– PvDev

Is your problem the dropdown or setting the values to the right section? 您的问题是否在下拉列表中或将值设置为正确的部分? Maybe this will help. 也许这会有所帮助。 Not sure if i understood your question: 不知道我是否理解您的问题:
Jquery jQuery的

$(".dropdown-menu a").click(function(){
    var selText = $(this).text();
    var currentBtn = $(this).closest(".btn-group").children("button");
    var currentUnit = currentBtn.data("unit");
    currentBtn.html(selText);
        console.log(currentUnit);
    $("."+ currentUnit).text(selText);
   });

Html HTML

<div class="btn-group btn-group-sm" role="group">
  <button id="startHour" type="button" data-unit="hours"  class="btn btn-secondary" data-toggle="dropdown" href="#">hour</button>
  <div class="dropdown-menu" aria-labelledby="startHour">
    <a class="dropdown-item" href="#">1</a>
    <a class="dropdown-item" href="#">2</a>
    <a class="dropdown-item" href="#">3</a>
    <a class="dropdown-item" href="#">4</a>
    <a class="dropdown-item" href="#">5</a>
    <a class="dropdown-item" href="#">6</a>
    <a class="dropdown-item" href="#">7</a>
    <a class="dropdown-item" href="#">8</a>
    <a class="dropdown-item" href="#">9</a>
    <a class="dropdown-item" href="#">10</a>
    <a class="dropdown-item" href="#">11</a>
    <a class="dropdown-item" href="#">12</a>
  </div>
  <span style="color:white; background-color:#5A6268">:</span>
</div>
<div class="btn-group btn-group-sm" role="group">
  <button id="startMinute" type="button" data-unit="minutes" class="btn btn-secondary" data-toggle="dropdown" href="#">min</button>
  <div class="dropdown-menu" aria-labelledby="startMinute">
    <a class="dropdown-item" href="#">00</a>
    <a class="dropdown-item" href="#">05</a>
    <a class="dropdown-item" href="#">10</a>
    <a class="dropdown-item" href="#">15</a>
    <a class="dropdown-item" href="#">20</a>
    <a class="dropdown-item" href="#">25</a>
    <a class="dropdown-item" href="#">30</a>
    <a class="dropdown-item" href="#">35</a>
    <a class="dropdown-item" href="#">40</a>
    <a class="dropdown-item" href="#">45</a>
    <a class="dropdown-item" href="#">50</a>
    <a class="dropdown-item" href="#">55</a>
  </div>
</div>
<div class="output">
  <button type="button" class="hours btn btn-secondary">am</button>
  <button type="button" class="minutes btn btn-secondary">zone</button>
  <button type="button" class="seconds btn btn-primary">Set</button>
</div>

https://jsfiddle.net/Rakowu/t982sLuj/1/ https://jsfiddle.net/Rakowu/t982sLuj/1/

I gave your group elements a data attr to figure out what kind of unit your clicking on. 我给您的组元素一个数据属性,以确定您单击的单位是什么。 Than i select the output element with the callback. 比我选择带有回调的输出元素。 Hope it will help (If there are ideas on how reduce my code i'll glad to here about it :D) 希望对您有所帮助(如果对减少代码量有想法,我很乐意在此进行:D)

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

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