Warm tip: This article is reproduced from serverfault.com, please click

javascript-选择时显示选定的下拉项

(javascript - On select to show the selected dropdown item)

发布于 2020-11-29 09:30:58

我有一个带有下拉列表的搜索栏引导程序代码,但是我想在从下拉列表中选择一个选项时显示该选项。当前,它显示“全部”,甚至你单击下拉列表并选择任何不起作用的选项。任何使用JavaScript或jQuery使其工作的人

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap 4 Search Box With Dropdown List</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

  <body>
    <div class="search-box col-md-5">
      <form action="">
        <div class="input-group mb-3">
          <div class="input-group-prepend">
            <button class="btn btn-light dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All</button>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">Category One</a>
              <a class="dropdown-item" href="#">Category Two</a>
              <a class="dropdown-item" href="#">Category Three</a>
              <div role="separator" class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Separated Category</a>
            </div>
          </div>
          <input type="text" class="form-control" aria-label="Search input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-success" type="button">Search</button>
          </div>
        </div>
      </form>
    </div>
  </body>

</html>
Questioner
Omer Farooq Butt
Viewed
11
Alen.Toma 2020-11-29 17:49:30

你可以简单地添加点击事件

dropdown-item

见下文

$('.dropdown-menu .dropdown-item').click(function(){
     var value = $(this).text();
     console.log(value)
     // Update the value and show it to the user
     $(this).parent().parent().find(".dropdown-toggle").html(value);

});
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap 4 Search Box With Dropdown List</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

  <body>
    <div class="search-box col-md-5">
      <form action="">
        <div class="input-group mb-3">
          <div class="input-group-prepend">
            <button class="btn btn-light dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All</button>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">Category One</a>
              <a class="dropdown-item" href="#">Category Two</a>
              <a class="dropdown-item" href="#">Category Three</a>
              <div role="separator" class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Separated Category</a>
            </div>
          </div>
          <input type="text" class="form-control" aria-label="Search input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-success" type="button">Search</button>
          </div>
        </div>
      </form>
    </div>
  </body>

</html>