一个多星期以来,我一直在尝试不同的选项,但似乎什么都不起作用。更复杂的是,我在页面上有多个表单,它们都需要绑定到同一个提交函数。他们都有不同的身份证。
以下是我的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',
cache: false,
url: 'inc/process.php',
data: data,
success: function(){
console.log("Updates have successfully been ajaxed");
}
});
return false;
});
当我通过tinyMce传递Ajax字符串数据时,我很困惑。。但是它不能用php保存到数据库中。。。然后我用
tinyMCE.triggerSave();
event.preventDefault();
那么罚款。。。。。。。。。
$("#save").click(function() {
tinyMCE.triggerSave();
event.preventDefault();
var data = $(this).serialize();
var position = $("#position").val();
var location = $("#job_location").val();
|
|
|
|
var end_date = $("#end_date").val();
var dataString = '&position='+ position + '&job_location=' + location + '&job_category=' + category + '&job_des=' + job_des +'&job_res='+ job_res + '&job_requ='+ job_requ + '&start_date='+ start_date + '&end_date='+ end_date;
alert(dataString);
$.ajax({
type: "POST",
url: "regis.php",
data: dataString,
success: function(data){
}
});
return false;
});
我认为问题是你没有阻止表单的默认操作。试试这个
$('form').bind( 'submit', function(event) {
event.preventDefault(); // added
console.log("Binding"); // changed to console.log
$.ajax({
type: "POST",
url: "inc/process.php",
data: $(this).serialize(),
success: function() {
console.log("Your updates have successfully been added."); // changed to console.log
}
});
});
另一个巧妙的技巧是在tinymce编辑器上设置进度状态,为您提供了一种添加加载图标的非常简单的方法。TinyMCE文档中的这篇文章解释了如何做到这一点。
同样在这篇文章中,使用ed.setContent()可以设置编辑器中显示的文本。我用它来清空编辑,但只是在一篇成功的帖子之后。