无法给出固定位置以在下拉菜单中添加新选项

时间:2018-12-13 06:51:15

标签: jquery html css jquery-ui semantic-ui

我正在使用带有以下代码的语义ui下拉列表

$('.ui.dropdown')
  .dropdown()
;
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
  </head>
  <body>

    <select class="ui dropdown">
      <option value="">Select option</option>
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
      <option value="4">Option 4</option>
      <option value="5">Option 5</option>
      <option value="6">Option 6</option>
      <option value="new">+Add new</option>
    </select>

</body>
</html>

我无法为最后一个选项提供固定位置。我希望最后一个选项“ +添加更多”具有固定位置,并且不应与其他选项一起滚动。我希望添加新选项就像在this image

中一样

2 个答案:

答案 0 :(得分:1)

您可以使用css属性sticky这样定义它:

$('#sticky').bind('click', function(event) {
  //do what you want
  var x = document.getElementById("industries_served");
  var option = document.createElement("option");
  option.text = prompt("Please enter a new value", "Florida");
  x.add(option);
});
* {
  font-size: 16px;
}

#sticky {
  position: -webkit-sticky;
  /* Safari */
  position: sticky;
  bottom: 0;
  border-top: .5px solid;
  color: blue;
}

select {
  border: none;
}

#wrapper {
  border: 1px solid;
  width: fit-content;
  margin: auto;
  /* just here to center the div*/
}

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  z-index: 1;
}

#industries_served option:hover {
  background-color: #ddd;
}

#sticky:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <div id="wrapper">
      <select class="ui fluid search dropdown" id="industries_served" name="industries_served" multiple="">
        <option value="">State</option>
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
        <option value="AZ">Arizona</option>
        <option value="AR">Arkansas</option>
        <option value="AR">New Mexico</option>
        <option value="AR">New York</option>
        <option value="AR">???</option>
      </select>
      <div id="sticky">+ Add more</div>
    </div>
  </div>
</div>

答案 1 :(得分:0)

请尝试以下代码:

<!DOCTYPE html> 
<html>

<head>
<meta charset="UTF-8">
<style>

    select {
        position: relative;
    }
    select option {
        position: relative;
    }
    select option:last-child {
        position: sticky;
        bottom: 0;
        background-color: red;
    }
    </style>
 </head>
 <body>
    <select class="ui fluid search dropdown" id="industries_served" name="industries_served" multiple="">
        <option value="">State</option>
        <option value="AL">Alabama</option>
        <option value="AK">Blaska</option>
        <option value="AZ">CArizona</option>
        <option value="AR">DArkansas</option>
        <option value="AR">+Add more</option>
    </select>

    </body>

   </html>