用所选选项的文本替换引导程序下拉默认文本

时间:2018-09-19 23:07:14

标签: javascript html css bootstrap-4

如何根据用户的选择使引导下拉列表替换默认文本?我只能在选择框内找到有关此操作的建议,而在Bootstrap 4下拉菜单中则找不到。

当用户选择一个下拉选项时,我希望以“ Please Select”开头。当前,当用户做出选择时,下拉菜单将再次被隐藏。

        <div class="dropdown">
                <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Please Select //here is where I would like to be updated
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
                  <button class="dropdown-item" type="button">Action</button>
                  <button class="dropdown-item" type="button">Another action</button>
                  <button class="dropdown-item" type="button">Something else here</button>
                </div>
       </div>

1 个答案:

答案 0 :(得分:0)

下拉菜单并不适合此操作。也许您应该考虑改用输入选择?

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container">
  <div class="form-group">
    <label for="sel1">Please Select</label>
    <select class="form-control" id="sel1">
      <option value="" disabled selected>Select your option</option>
      <option>Action</option>
      <option>Another action</option>
      <option>Something else here</option>
    </select>
  </div>
</div>