I am trying to submit my form in AJAX, so I have to serialize() the data. But I am using fckEditor and jQuery doesn't know how to deal with it, so after the serialization, I am trying to manually modify the value, but no luck so far... any ideas

if(content_val!=""){
    var values = $("#frmblog").serialize();
    values.content = content_val; //content_val is the manually fetched data which I am trying to insert into the serialized content.
    alert(content_val); alert(values);
}

推荐答案

serialize returns a URL-encoded string containing the form fields. If you need to append to it, you do so using the standard URL-encoded string rules, e.g.:

var values = $("#frmblog").serialize();
values += "&content=" + encodeURIComponent(content_val);

(以上假设在serialize调用之后,values中总会有一个值;如果这不一定是真的,那么在添加之前,请根据values是否为空来确定是否使用&.)

Alternately, if you like, you can use serializeArray and then add to the array and use jQuery.param to turn the result into a query string, but that seems a long way 'round:

// You can also do this, but it seems a long way 'round
var values = $("#frmblog").serializeArray();
values.push({
    name: "content",
    value: content_val
});
values = jQuery.param(values);

Update: In a comment added later you said:

问题是,在序列化过程中,在‘Content’键中设置了一些默认值,所以我不能只附加一个新值,我必须更新其中已有的值."

That changes things. It's a pain to look for content within the URL-encoded string, so I'd go with the array:

var values, index;

// Get the parameters as an array
values = $("#frmblog").serializeArray();

// Find and replace `content` if there
for (index = 0; index < values.length; ++index) {
    if (values[index].name == "content") {
        values[index].value = content_val;
        break;
    }
}

// Add it if it wasn't there
if (index >= values.length) {
    values.push({
        name: "content",
        value: content_val
    });
}

// Convert to URL-encoded string
values = jQuery.param(values);

You'd probably want to make this a reusable function.

Jquery相关问答推荐

添加动态表单后,Select2 消失

在 div 中找到第一次出现的类

在 Bootstrap 3 中向工具提示添加换行符

$.post 和 $.ajax 之间的区别?

删除所有子元素的 CLASS

在单击事件上判断 Ctrl / Shift / Alt 键

使用 jquery/ajax 刷新/重新加载 Div 中的内容

虽然未定义变量 - 等待

如何使用 jQuery 停止默认链接点击行为

所有但不是jQuery Select 器

将片段添加到 URL 而不导致重定向?

jQuery 日期 Select 器 - 禁用过go 的日期

如何删除/更改 JQuery UI 自动完成助手文本?

什么时候应该使用 jQuery deferred 的then方法,什么时候应该使用pipe方法?

jquery,id内类的 Select 器

调整浏览器大小时如何自动居中 jQuery UI 对话框?

检测页面是否已完成加载

默认情况下如何将tinymce粘贴为纯文本

带有 LIKE 的 Spring JPA @Query

jquery - 成功时使用ajax结果返回值