使用表格中的多个下拉菜单作为选择列表

时间:2018-02-25 20:08:37

标签: jquery twitter-bootstrap bootstrap-4

所以我有一个用户表,我想在表中添加一列,我可以使用bootstrap下拉菜单将用户状态更改为活动/非活动状态。这是表格的代码:

<table class="table">
<thead>
  <tr>
    <th>#</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Status</th>
  </tr>
</thead>
<tbody>
  <tr>
    <th scope="row">1</th>
    <td>Mark</td>
    <td>Otto</td>
    <td>
      <div class="dropdown">
        <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Active
        </button>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
          <a class="dropdown-item" href="#">Active</a>
          <a class="dropdown-item" href="#">Inactive</a>
        </div>
      </div>
    </td>
  </tr>
  <tr>
    <th scope="row">2</th>
    <td>Jacob</td>
    <td>Thornton</td>
    <td>
      <div class="dropdown">
        <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Active
        </button>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
          <a class="dropdown-item" href="#">Active</a>
          <a class="dropdown-item" href="#">Inactive</a>
        </div>
      </div>
    </td>
  </tr>
  <tr>
    <th scope="row">3</th>
    <td>Larry</td>
    <td>the Bird</td>
    <td>
      <div class="dropdown">
        <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Active
        </button>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
          <a class="dropdown-item" href="#">Active</a>
          <a class="dropdown-item" href="#">Inactive</a>
        </div>
      </div>
    </td>
  </tr>
</tbody>

我在最后添加了一个JQuery片段。此代码段的作用是根据点击的dropdown-menu更改dropdown-item标签。

$(function(){
   $(".dropdown-menu").on('click', 'a', function(){
   $(".btn:first-child").text($(this).text());
   $(".btn:first-child").val($(this).text());
   });
});

当我更改一个记录的状态时,所有其他记录都会随着该更改而生效。我尝试使用.find().siblings()等功能,甚至取消id="dropdownMenuButton"但无法解决问题。有人可以帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

看起来您并不了解jQuery代码段的工作原理。它应该是:

     <!DOCTYPEhtml>
     <head>


    <title>Daniel Vaughan</title>
    <meta charset="utf-8">
    <link href="https://fonts.googleapis.com/css?family=Abel|Archivo+Narrow|Barlow|Cabin|Courgette|Dancing+Script|Kanit|Questrial" rel="stylesheet">

     <meta name="viewport" content="width=device-width, initial-scale=1">
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
    </script>
    </script>

    <style>

    body{
    background-image:url(images/Omen-Background.jpg);
    background-position: center;
    color: red;
     font-family: 'Barlow','arial', 'Cabin';
    font-weight: bold;
    font-size: 18px;
    }

    .navbar{
     background-image: inherit;
     color:inherit;
     }

    #aboutme{
        margin: 50px 40px 40px 50px;
        padding: 50px 25px 25px 50px;
        background-image: inherit;
        color: grey;
        text-shadow: 1px 1px 2px red;
        font-family: 'Barlow';
        font-size: 18px;
    }

    p{
        background: rgba("204, 204, 204, 0.5");
        color: grey;
        text-shadow: 1px 1px 2px red;
    }

    </style>
    </head>

        <body>
     <div class="container-fluid">

     <div class="row">
      <div class="col-md-6 text-left">
     <img class="title-logo" src="https://www.w3schools.com/howto/img_paris.jpg" height="70px" width="100x" alt="Omen logo">

      </div>
      <div class="col-md-6 text-right text-uppercase">
          <h1 class="text-super">Daniel Vaughan</h1>
          <h4>Freelance Developer</h4>

        </div>
        </div>
<header>
      <div class="container">
<div class="dropdown">
 <button class="btn dropdown-toggle" data-toggle="dropdown">
   <span class="caret"></span></button>
     <ul class="dropdown-menu">
       <a href="danvaughan.html"><li>Home</li></a>
       <a href="projects.html"><li>Projects</li></a>
       <a href="employment.html"><li>Employment History</li></a>
       <a href="hobbies.html"><li>Hobbies</li></a>
      </ul>
      </div>
</div>

</header>
<div class="container">
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators black">
      <li data-target="#myCarousel" data-slide-to ="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to ="1"></li>
      <li data-target="#myCarousel" data-sldie-to ="2"></li>
      <li data-targert="#myCarousel" data-slide-to ="3"></li>
    </ol>

<div class="carousel-inner">
    <div class="item active">
      <img src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg"  width="100%">
    <div class="carousel-caption">
      <h3>First family holiday</h3>
    </div>
</div>

<div class="item">
    <img class="image-responsive" src="https://cdn.pixabay.com/photo/2013/04/06/11/50/image-editing-101040_960_720.jpg" width="100%">
  <div class="carousel-caption">
    <h3>My Favourite Hobbie</h3>
  </div>
</div>

<div class="item">
    <img class="image-responsive" src="https://html5box.com/html5lightbox/images/lakeandballoon.jpg" width="100%">
  <div class="carousel-caption">
    <h3>Some of my favourite books</h3>
  </div>
</div>

<div class="item">
  <img class="image-responsive" src="https://demo.phpgang.com/crop-images/demo_files/pool.jpg" width="100%">
    <div class="carousel-caption">
      <h3>Gaming (my second favourite hobbie)</h3>
      </div>
</div>

  <a class="left carousel-control" href="#myCarousel" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span>
  </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span>
    </a>

</div>

    <div class="container-fluid">
    <div class="row">

        <div class="col-6 text-left" id="aboutme">
        <h1> A Little About Me</h1>
            <p class="text-left">Hello<br/> My name is Daniel Vaughan (Dan). Welcome to my website.</p>

            <p class="text-left">I am a freelance web developer based in Birmingham UK. I have gained experience in the field of web development through the good people  at Freecode camp, where I learned how to code. I love coding.<br/> It gives me great satisfaction when I make websites that are responsive and beautiful.At the moment I am in the market for part-time work as I am still employed as a machine operator.</p>
        </div>
        </div>
    </div>

    <script>

        $("document").ready(function(){
            $("#item").click(function(){
                $("#submenu").slideToggle(500);
            });

        });


    </script>

    </body>

https://www.codeply.com/go/SkKl0OPReI