簡體   English   中英

從表的選定行中獲取所有值

[英]get all values from a selected row of the table

如何從表2到表1插入一些值。如果單擊“添加”按鈕,我想獲取所選行的所有值。 我應該從我選擇的當前行中獲取rowindex並通過rowindex獲取$prod值嗎?

<table id="table1">
    <tr>
        <th>Product ID</th>
        <th>Product Name</th>
        <th>Price</th>
        <th>Stock</th>
        <th>Action</th>
    </tr>
    <?php foreach($prod->getAll() as $name):?>
    <tr>
        <td><?php echo $name['prod_id'];?></td>
        <td><?php echo $name['name'];?></td>
        <td><?php echo $name['price'];?></td>
        <td><?php echo $name['stock'];?></td>
        <td><button class="addbtn">Add</button></td>
    </tr>
    <?php endforeach;?>
</table>

<table id="table2">
<tr>
    <th>No</th>
    <th>Product Name</th>
    <th>Price</th>
    <th>Qty</th>
    <th>Total</th>
</tr>
<tr>
    <td>1</td>
    <td>Some Product Name From Table 1</td>
    <td>Some Product Price From Table 1</td>
    <td>Qty is generated from table 1</td>
    <td>Qty * Price</td>
</tr>
</table>


 <?php

 class Product extends DB_Connect
 {

    public function __construct()
    {
     parent::__construct();
    }

function getAll(){
    try
    {
        $sql = "SELECT * FROM product Order By prod_id asc";
        $result = $this->db->query($sql);
        $results = $result->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    }
    catch ( Exception $e )
    {
        die ( $e->getMessage() );
    }
}
}

?>

如果您使用的是jQuery,則可以使用closest()函數:

$('.addbtn').click(function(){
    var row = $(this).closest('tr'),
        cells = row.find('td'),
        prodId = cells.eq(0).html(),
        name = cells.eq(1).html(),
        price = cells.eq(2).html(),
        stock = cells.eq(3).html();
});

小提琴: http : //jsfiddle.net/Afk8a/1/

暫無
暫無

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

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