简体   繁体   English

Append / 从 javascript / jQuery 的数据库中复制带有下拉值的表行

[英]Append / copy table row with a dropdown value from database in javascript / jQuery

I have a dropdown value from database in table row,我在表行中有一个来自数据库的下拉值,

<button type="button" class="btn btn-outline-primary" id="addbutton">Add Item</button>
<table class="table" id="table">
      <thead>
        <tr>
          <th scope="col">Name</th>
        </tr>
      </thead>
      <tbody>
        <tr>
            <td>
            <select >
              <option selected="" disabled="">--Select Product Name--</option>
                <?php
                $con = new mysqli($host, $dbid, $dbpass, $dbname);
                $stmt = $con->prepare( "SELECT name FROM product ORDER BY name DESC" );
                $stmt->execute();
                $result = $stmt->get_result();
                $con->close();
                while($row = mysqli_fetch_assoc($result)) {
                  echo '<option value="'.$row["name"].'">'.$row["name"].'</option>';
                }
              ?>
            </select>
            </td>
        </tr>
        </tbody>
    </table>

if I click add button, I want to append the row that have same dropdown without make a database query again (for a dropdown value).如果我单击添加按钮,我想 append 具有相同下拉列表的行而不再次进行数据库查询(对于下拉值)。 I have make jQuery to add row我已经制作了 jQuery 来添加行

  $("#addbutton").click(function(){
    $('#table tr:last').after(' _??_ ');
  });

what is the best way to do this?做这个的最好方式是什么?

You can have implemented by selecting the <tr> that contains the select then .clone() it and using .appendTo() you can have added to the table, here is a working snippet:您可以通过选择包含select然后.clone()它的<tr>并使用.appendTo()来实现,您可以添加到表中,这是一个工作片段:

 $('#addbutton').click(function() { $('table tr:last-child').clone().appendTo($('table')) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" class="btn btn-outline-primary" id="addbutton">Add Item</button> <table class="table" id="table"> <thead> <tr> <th scope="col">Name</th> </tr> </thead> <tbody> <tr> <td> <select> <option selected="" disabled="">--Select Product Name--</option> <option selected="" disabled="">option1</option> <option selected="" disabled="">option2</option> <option selected="" disabled="">option3</option> <option selected="" disabled="">option4</option> </select> </td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM