亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 javascript / jQuery 中使用數據庫中的下拉值追加/復制表行

在 javascript / jQuery 中使用數據庫中的下拉值追加/復制表行

PHP
互換的青春 2023-04-23 17:44:39
我在表行中有一個來自數據庫的下拉值,<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>如果我單擊添加按鈕,我想附加具有相同下拉列表的行而不再次進行數據庫查詢(對于下拉值)。我讓 jQuery 添加行  $("#addbutton").click(function(){    $('#table tr:last').after(' _??_ ');  });做這個的最好方式是什么?
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您可以通過選擇包含<tr>然后select.clone()并使用.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>


查看完整回答
反對 回復 2023-04-23
  • 1 回答
  • 0 關注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號