我試圖在表格#itemsTable中存儲內容tr,在數組celValues中並將其發佈到php並重復它(在下一個tr之前直到最後一個tr)。 這裏是代碼:分開發布html表的行到php
$('#save').click(function(){
//alert("hi");
var celValues= new Array();
$('#itemsTable tr').each(function() {
celValues[0] = $('td:nth-child(2)').find('input').val();
celValues[1] = $('td:nth-child(3)').find('input').val();
celValues[2] = $('td:nth-child(4)').find('input').val();
celValues[3] = $('td:nth-child(5)').find('input').val();
celValues[4] = $('td:nth-child(6)').find('input').val();
$.post("test.php", {celvalues: celValues},function(data){
document.write(data);
});
});
});
test.php的:
<?php
$i=0;
$arr= array();
$arr = $_POST['celvalues'];
foreach($arr as $val)
{
echo " " .$arr[$i];
$i++;
}
?>
問題是重複的,而不是發佈所有TR的內容第一行。
輸出3行具有不同contents.but第一行重複3次:
1000網站設計1 100.5 100.5 1000Website Design1100.5100.5 1000網站設計1 100.5 100.5
仔細檢查foreach循環,你打印'$ arr'而不是'$ val'。另外,一個好的調試技術是使用'print_r($ array)'來查看數組的原始內容。 – Moshe 2012-02-14 05:15:39