我已经尝试了一个多星期的不同选择,似乎没有任何工作.使这稍微复杂一点的是我在页面上有多个表单,所有表单都需要绑定到同一个提交函数.它们都有不同的ID.
以下是我的jQuery的简化版本:
$('form').on('submit',function(form){ var data = $(this).serialize(); $.ajax({ type: 'POST',cache: false,url: 'inc/process.PHP',data: data,success: function(){ // The following fires on first AND second submit console.log("Updates have successfully been ajaxed"); } }); return false; });
我也试过使用$(‘form’).submit()得到相同的结果.
process.PHP的相关部分:
$query = 'UPDATE pop_contents SET '; $id = $_POST['content_id']; /* to avoid including in MysqL query later */ unset($_POST['content_id']); $length = count($_POST); $count = 0; foreach($_POST as $col => $value){ $value = trim($value); $query .= $col."='".escapeString($value); // don't add comma after last value to update if(++$count != $length){ $query .= "',"; } // add space before WHERE clause else{ $query .= "' "; } } $query .= 'WHERE id='.$id; $update_result = $MysqLi->query($query);
经过多次拔毛和咒骂后,我解决了这个问题.
TinyMCE编辑器实例不直接编辑textareas,所以为了提交表单,我需要先从TinyMCE API调用tinyMCE.triggerSave().因此,工作代码如下所示:
$('form').on('submit',function(form){ // save TinyMCE instances before serialize tinyMCE.triggerSave(); var data = $(this).serialize(); $.ajax({ type: 'POST',success: function(){ console.log("Updates have successfully been ajaxed"); } }); return false; });