我试图在 $.post 调用中包含两个变量和一个序列化表单,而我发现这样做的唯一方法是使用 serializeArray() 而不是 serialize(),然后将额外的变量添加到大批。所有表单元素都被正确处理,但额外的两个变量(订单和列)没有。
JQ:
var order = 'asc';
var column = 'description';
$('#task_form').submit(function(e){
var formData = $("#task_form").serializeArray();
formData.push({column: column, order: order});
$.post('process.php', formData, function(data){
// clear the current task table
$('#tbody').empty();
// refresh the task table with the newly inserted task
$(data).appendTo('#tbody');
$('#tasks_table').trigger('update');
$("#description_text").val('');
});
e.preventDefault();
});
PHP:
if(isset($_POST['description_text'])){
$description = $_POST['description_text'];
$duration = $_POST['duration_text'];
$deadline = $_POST['deadline_text'];
$order = $_POST['order'];
$column = $_POST['column'];
TaskDB::createLog($order.' '.$column);
// convert the date
$pieces = explode('/', $deadline);
$month = $pieces[0];
$day = $pieces[1];
$year = $pieces[2];
$newDate = $year.'-'.$month.'-'.$day;
// create a new task object, add it to database
$task = new Task('DEFAULT', $description, $duration, $newDate, '0');
TaskDB::addTask($task);
TaskDB::generateTaskTable();
}
有谁知道这是为什么?