簡體   English   中英

從下拉菜單中選擇選項時,如何在表格中添加另一行?

[英]How do I add another row in the table on select an option from a drop down?

這是我的桌子的樣子: 在此處輸入圖片說明

這是我的代碼:

    <table class="inventory" style="float:left;">
  <tbody>
    <tr>
        <th>Date bought</th>
      <th>Item</th>
      <th>Quantity</th>
      <th>Total</th>
      </tr>

    <tr>
        <td>
             <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
        <link rel="stylesheet" href="style.css">
        <script>
        $(document).ready(function() {
            $( "#datepicker" ).datepicker();
        });
        </script>
        <input type="text" id="datepicker"/>
        </td>
      <td>
          <select name="item_name_update" value="item_name_update" class="form-control ddplaceholder" style="width:220px; padding-left:40px;font-size:18px;font-family:Roboto;">
                   <option value="" disabled selected>Select Item</option>
            <?php
            $sth = $conn->prepare('Select item_name From item');
            $sth->execute();
            $data = $sth->fetchAll();   
            foreach ($data as $row ){
                if($row['item_name']!="")
             echo ' <option id=\"ItemName\" name=\"ItemName\" value="' .$row['item_name']. '">'.$row['item_name'].'</option>';
            }?>
            </select>
      </td>
      <td>
        <input type="text" placeholder="Quantity" name="Quantity" id="Quantity"/>
      </td>
      <td>
       <input type="text" placeholder="Total" value=""/>
      </td>
    </tr>
         <?php $totalCost="";
            if(!empty($_POST['item_name_update']) && !empty($_POST['datepicker']) && !empty($_POST['Quantity'])){
           $sth = $conn->prepare('Select price_per_unit From bill where item=:item and date=:date');
            $sth->execute(array(':item'=>$_POST['item_name_update'], ':date'=>$_POST['datepicker']));
            $data = $sth->fetchAll();   
            foreach ($data as $row ){
         $totalCost=($row['price_per_unit']*$_POST['Quantity']);
            }}
                ?>
  </tbody>
</table>

每次從下拉列表中選擇一項時,我想在表中添加一行。 我怎么做? 有沒有簡單的解決方案? 是否在選擇下拉列表中添加循環並添加document.change?

另外,我要添加所有“總計”文本框的總數。

編輯:

我下面有一些代碼,添加了新行。 但是,當我從最新行的下拉列表中選擇一個值時,應該添加新行。 現在,它僅添加了FIRST下拉菜單。 在此處輸入圖片說明

是的,我們可以使用insertAfter方法來實現。 您應該通過以下方式在選擇標簽更改時調用它:

$(function() {
   $("select").change(function() {
       $('<tr><td>new td1</td><td>new td2</td></tr>').insertAfter($(this).closest('tr'));
   });
});

希望對您有所幫助:)這是Jsfiddle

使用此代碼。

$(function() {$(".ddplaceholder").change(function() {
   $('<tr><td>new td1</td><td>new td2</td></tr>').insertAfter($(this).closest('tr')); }); });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM