【问题标题】:How to post html table data as a array如何将html表格数据作为数组发布
【发布时间】:2014-07-04 21:45:20
【问题描述】:

这是我的 html 表格代码,其中包含用于填充 html 表格数据的 php foreach 循环, 我想通过 form.serialize 将整个表数据作为数组发布到我的控制器 在阿贾克斯, 我可以这样做吗?怎么做??

<table class="table table-striped table-vcenter table-bordered">
                        <thead>
                        <th>Master Name</th>
                        <th>From No.</th>
                        <th>To No.</th>
                        <th>Total</th>
                        </thead>
                        <tbody>
                            <?php foreach ($master as $name) { ?>
                                <tr class="rc1_tr">
                                    <td class="td_amount"><div id="amount"><?php echo $name['amount']; ?></div></td>
                                    <td id="form_no"><div id="div_to_no"><?php echo $name['receipt_no_to'] ? $name['receipt_no_to'] : '0'; ?></div></td>
                                    <td align='center' class="qt" id="<?php echo $name['id'] ?>" style="width: 250px;"><input type="text" id="to_no" class="quantity" value="" style="text-align:center;width:180px;height:26px;margin-bottom: 2px;"/></td>
                                    <td id="td_total"><div id="total"></div></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                        <tfoot>
                            <tr class="grand_total">
                                <td colspan="3">Grand Total</td>
                                <td><div id="div_grand_total"></div></td>
                            </tr>
                        </tfoot>
                    </table>

【问题讨论】:

标签: php jquery html ajax


【解决方案1】:

你可以序列化所有输入,试试这个

//dynamic query_string generation
var filters_query_string = "&" + $(".rc1_tr input").map(function (i,e) {
    return $(e).serialize()
    }).toArray().join("&")

$.ajax({url  : $("#your_form").attr('action') +filters_query_string, 
        //other ajax params
        success: function(data) {
            //do stuff
        },
        dataType: 'json' /* return type, f.e: json  */,
        error: function(error) {
           console.log(error);                   
        },
})        

【讨论】:

  • 我在表格中没有输入,它只包含简单的文本。
【解决方案2】:

以这种方式尝试.. 将其内容放入 javascript 对象中

var rowIndex=0;
var columnIndex=0;
var tableJson = [];
$(".table tr").each(function () {
  var row = $(this);
  $("td", row).each(function () {
    var tdElem= $(this);
    tableJson.push({row=rowIndex,column=columnIndex,value=tdElem.html()});
    columnIndex+=1;
  });
 rowIndex+=1;
});

将此代码放入函数中

function getJsonTable(){
   //previous code
  return JSON.stringify(tableJson);
}

并将结果作为 jquery ajax 调用的data 参数传递

【讨论】:

  • 它给 tableJson 是未定义的,不能设置属性 0
  • 你把这个var tableJson = new Object();放了吗?
  • 是的,但你必须添加,tableJson[rowIndex] = new Object();,它可以工作
猜你喜欢
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2020-02-27
  • 2023-04-05
  • 2021-11-15
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
相关资源
最近更新 更多