Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

javascript - Having multiple dropdowns changes both of the dropdown buttons

How can I fix this issue? The libraries I use are JQuery and Bootstrap. I will post code showing what I have now.

Here is a video showing this

<div class="input-group-prepend ">
    <button aria-expanded="false"
    aria-haspopup="true" class="btn btn-light dropdown-toggle" data-toggle="dropdown" type="button">
      List type
    </button>
      <div class="dropdown-menu">
        <a class="dropdown-item">Watching</a>
        <a class="dropdown-item">Completed</a>
        <a class="dropdown-item">On-Hold</a>
        <a class="dropdown-item">Dropped</a>
        <a class="dropdown-item">Plan to Watch</a>
      </div>
  </div>

  <div class="dropdown">
    <button aria-expanded="false"
    aria-haspopup="true" class="btn btn-lg btn-light dropdown-toggle" data-toggle="dropdown" type="button">
    Format
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item">TV</a>
      <a class="dropdown-item">Movie</a>
      <a class="dropdown-item">Special</a>
      <a class="dropdown-item">OVA</a>
      <a class="dropdown-item">ONA</a>
      <a class="dropdown-item">Music</a>
    </div>
  </div>

  $(".dropdown-menu a ").on("click", function() {
    $("button.dropdown-toggle").text($(this).text());
    status = $("button.dropdown-toggle").text();
});
question from:https://stackoverflow.com/questions/65933280/having-multiple-dropdowns-changes-both-of-the-dropdown-buttons

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hey I think you have to make proper structure first as you can see in your code there are different structure for dropdown.

Also you need to make some JS changes as I have done.

here is a working code as you want in the following snippet.

$(".dropdown-menu a").on("click", function() {
  $(this).parent(".dropdown-menu").prev("button.dropdown-toggle").text($(this).text());
  status = $("button.dropdown-toggle").text();
});
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<body>
  <div class="input-group-prepend">
    <div class="dropdown">
      <button aria-expanded="false" aria-haspopup="true" class="btn btn-light dropdown-toggle" data-toggle="dropdown" type="button">List type</button>
      <div class="dropdown-menu">
        <a class="dropdown-item">Watching</a>
        <a class="dropdown-item">Completed</a>
        <a class="dropdown-item">On-Hold</a>
        <a class="dropdown-item">Dropped</a>
        <a class="dropdown-item">Plan to Watch</a>
      </div>
    </div>

    <div class="dropdown">
      <button aria-expanded="false" aria-haspopup="true" class="btn btn-light dropdown-toggle" data-toggle="dropdown" type="button">Format</button>
      <div class="dropdown-menu">
        <a class="dropdown-item">TV</a>
        <a class="dropdown-item">Movie</a>
        <a class="dropdown-item">Special</a>
        <a class="dropdown-item">OVA</a>
        <a class="dropdown-item">ONA</a>
        <a class="dropdown-item">Music</a>
      </div>
    </div>
  </div>
</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...