我有一个表,我正在根据PHP
. 这是一个可编辑的表格,当用户点击保存时,我需要遍历表格元素并将文本值存储到一个数组中。Job# 列中的值必须位于循环的每次迭代的开始或结束,以便我可以轻松提取它,然后在完成时使用它插入值。
PHP 表创建
<?php
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
$projects = getProjects($db);
$members = getUserName($db);
try {
$project_info = $db->prepare("
SELECT projects.id,
projects.project_name,
projects.pm,
projects.apm,
projects.est_start,
projects.est_end,
projects.contact,
projects.trips,
projects.tasks,
projects.perc_complete,
projects.bcwp,
projects.actuals,
projects.cpi,
projects.bcws,
projects.bac,
projects.comments,
projects.status,
projects.project_revenue,
projects.profit_margin,
projects.pm_perc,
projects.audited
FROM projects
WHERE account_id = ?
");
$project_info->bindValue(1, $aid, PDO::PARAM_STR);
$project_info->execute();
echo "<table class='projects_contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
echo "<th class='content_th'>" . "PM" . "</th>";
echo "<th class='content_th'>" . "APM" . "</th>";
echo "<th class='content_th'>" . "Est. Start" . "</th>";
echo "<th class='content_th'>" . "Est. End" . "</th>";
echo "<th class='content_th'>" . "Contact" . "</th>";
echo "<th class='content_th'>" . "Trips" . "</th>";
echo "<th class='content_th'>" . "Tasks" . "</th>";
echo "<th class='content_th'>" . "% Complete" . "</th>";
echo "<th class='content_th'>" . "BCWP" . "</th>";
echo "<th class='content_th'>" . "Actuals" . "</th>";
echo "<th class='content_th'>" . "CPI" . "</th>";
echo "<th class='content_th'>" . "Comments" . "</th>";
echo "<th class='content_th'>" . "Status" . "</th>";
echo "<th class='content_th'>" . "Project Revenue" . "</th>";
echo "<th class='content_th'>" . "Profit Margin" . "</th>";
echo "<th class='content_th'>" . "PM%" . "</th>";
echo "<th class='content_th'>" . "Audited" . "</th>";
while ($row = $project_info->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['id'] . "</a>" . "</td>";
echo "<td class='projects_editable_content_td' contenteditable='true'>" . $row['project_name'] . "</td>";
echo "<td class='projects_editable_content_td'>" .
"<select id='table_edit_project_pm'>" .
"<option value=''>" . $row['pm'] . "</option>" .
"<option>" . "-----" . "</option>";
foreach($members as $key => $value) {
echo "<option value='".$key."'>" . $value . "</option>";
}
echo "</td>";
echo "<td class='projects_editable_content_td'>" .
"<select id='table_edit_project_apm'>" .
"<option value=''>" . $row['apm'] . "</option>" .
"<option>" . "-----" . "</option>";
foreach($members as $key => $value) {
echo "<option value='".$key."'>" . $value . "</option>";
}
echo "</td>";
echo "<td class='projects_editable_content_td'>" .
"<input type='text' id='table_edit_project_start' value='". $row['est_start'] ."'>";
echo "</td>";
echo "<td class='projects_editable_content_td'>" .
"<input type='text' id='table_edit_project_end' value='". $row['est_end'] ."'>";
echo "</td>";
echo "<td class='projects_editable_content_td' contenteditable='true'>" . $row['contact'] . "</td>";
echo "<td class='content_td'>" . $row['trips'] . "</td>";
echo "<td class='content_td'>" . $row['tasks'] . "</td>";
echo "<td class='content_td'>" . $row['perc_complete'] . "</td>";
echo "<td class='content_td'>" . $row['bcwp'] . "</td>";
echo "<td class='content_td'>" . $row['actuals'] . "</td>";
echo "<td class='content_td'>" . $row['cpi'] . "</td>";
echo "<td class='projects_editable_content_td' contenteditable='true'>" . $row['comments'] . "</td>";
echo "<td class='projects_editable_content_td'>" .
"<select id='table_edit_project_status'>" .
"<option value=''>" . $row['status'] . "</option>" .
"<option>" . "-------" . "</option>" .
"<option>" . "On Hold" . "</option>" .
"<option>" . "In Progress" . "</option>" .
"<option>" . "Open" . "</option>" .
"<option>" . "Complete" . "</option>" .
"</select>";
echo "</td>";
echo "<td class='content_td'>" . $row['project_revenue'] . "</td>";
echo "<td class='content_td'>" . $row['profit_margin'] . "</td>";
echo "<td class='content_td'>" . $row['pm_perc'] . "</td>";
echo "<td class='projects_editable_content_td'>" .
"<input type='checkbox' id='table_edit_project_audited'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die($e->getMessage());
}
} else {
echo 'could not load projects table';
}
?>
现在我目前用来获取这些数据的代码看起来像这样
jQuery
$('.projects_editable_content_td, .projects_contentTable td:nth-child(3) a').each(function () {
saveEdits.push($(this).text());
});
// then alert the values using this loop
$.each(saveEdits, function(index, value) {
alert(index + ': ' + value);
});
当这个循环通过时,我在 saveEdits[0] 和 saveEdits[1] 上从 Job# 获取值,然后我从 [select] 元素中获取所有 [option] 值。我知道这是因为我需要以某种方式将“option:selected”合并到这个循环中。无论如何,我的循环是一团糟,在搞砸了几个小时后,我无法让这个东西正常工作。
再次,我需要遍历每个 tr,从 tr 内的每个 td 元素中获取文本值,并在循环的每次迭代开始或结束时存储来自 Job# 列的值。
非常感谢任何帮助