-1

如何获取任何表中选定行的内容并使用 JQuery 在 post 数组中提交它们的值?

这是我的桌子:

            <?php if(isset($products)){ ?>
            <?php foreach( $products as $product){ ?>
                <tr id="<?php echo $product['order_product_id']; ?>">
                    <td class="center"><input checked="checked" type="checkbox" name="selected-p[]" value="<?php echo $product['order_product_id']; ?>" /></td>
                    <td class="left"><input type="text" name="order_id" value="<?php echo $product['order_id']; ?>"/></td>
                    <td class="left"><input type="text" name="product_id" value="<?php echo $product['product_id']; ?>"/></td>
                    <td class="left"><input type="text" name="name" value="<?php echo $product['name']; ?>"/></td>
                    <td class="left"><input type="text" name="price" value="<?php echo $product['price']; ?>"/></td>

                    <td class="left"><input type="text" name="cost" value="<?php echo $product['cost']; ?>"/></td>
                    <td class="left"><input type="text" name="product_cost" value="<?php echo $product['product_cost']; ?>"/></td>
                    <td class="left"><input type="text" name="logistics_cost" value="<?php echo $product['logistics_cost']; ?>"/></td>
                    <td class="left"><input type="text" name="inventory_cost" value="<?php echo $product['inventory_cost']; ?>"/></td>

                    <td class="left"><font  dir="ltr"><?php echo $product['date_added']; ?></font></td>
                </tr>
            <?php } ?>
        <?php } ?>

问题作者呈现的 HTML:

<tr id="39118"><td class="center"><input type="checkbox" name="selected-p[]" value="39118"></td> <td class="left"><input type="text" name="order_id" value="10141"></td> <td class="left"><input type="text" name="product_id" value="881"></td> <td class="left"><input type="text" name="name" value="أساور الصداقة"></td> <td class="left"><input type="text" name="price" value="30.0000"></td> <td class="left"><input type="text" name="cost" value="9.0000"></td> <td class="left"><input type="text" name="product_cost" value="9.00"></td> </tr>
4

1 回答 1

1

试试这个...

var data = [];

$("#table1 tr").filter(function() {
    return $("td:eq(0) input[type=checkbox]", this).attr("checked");
}).each(function() {
    data.push({
        order_id: $("input[name=order_id]", this).val(),
        product_id: $("input[name=product_id]", this).val(),
        name: $("input[name=name]", this).val(),
        price: $("input[name=price]", this).val(),
        cost: $("input[name=cost]", this).val(),
        product_cost: $("input[name=product_cost]", this).val(),
        logistics_cost: $("input[name=logistics_cost]", this).val(),
        inventory_cost: $("input[name=inventory_cost]", this).val()
    });
});

console.log(data);

if (data.length > 0) {
    $.ajax({
        url: "http://www.webpage.com",
        type: "POST",
        data: data,
        success: function(data) {
            console.log("Success!! " + data);
        }
    });
}

它创建一个名为的数组data,并用代表被勾选的表格的每一行的对象填充它。(Console.log 会告诉你它得到了什么)。

然后它执行 ajax 调用,发布数据并显示返回的任何结果数据。

(我做了 1 个假设,您需要更改以适应。我假设该表的 id 为table1- 请参阅第二行代码。)

这是一个工作的jsfiddle

于 2012-11-23T17:44:12.257 回答