Blu*_*ica 78 jquery serialization fckeditor
我试图在AJAX中提交我的表单,所以我必须序列化()数据.但我正在使用fckEditor和jQuery不知道如何处理它,所以序列化后,我试图手动修改值,但到目前为止没有运气...任何想法
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);
}
Run Code Online (Sandbox Code Playgroud)
T.J*_*der 145
serialize返回包含表单字段的URL编码字符串.如果您需要附加到它,请使用标准的URL编码字符串规则,例如:
var values = $("#frmblog").serialize();
values += "&content=" + encodeURIComponent(content_val);
Run Code Online (Sandbox Code Playgroud)
(以上假设values在serialize调用之后总会有一个值;如果不一定是这样,请在添加之前&根据是否values为空来确定是否使用.)
或者,如果您愿意,可以使用serializeArray然后添加到数组并使用jQuery.param将结果转换为查询字符串,但这似乎很长一段时间:
// 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);
Run Code Online (Sandbox Code Playgroud)
更新:在稍后添加的评论中,您说:
问题是,在血清化过程中,在'content'键中设置了一些默认值,所以我不能只附加一个新值,我必须更新它中已有的值"
这会改变一切.content在URL编码的字符串中查找是很痛苦的,所以我会使用数组:
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);
Run Code Online (Sandbox Code Playgroud)
您可能希望将其设为可重用的功能.
现在有了 ES15。您可以使用它来代替编辑当前提交的值(最短的值)
var values = $("#frmblog").serializeArray();
values.find(input => input.name == 'content').value = content_val;
console.log(values);
Run Code Online (Sandbox Code Playgroud)
或本机函数
var values = $("#frmblog").serializeArray();
values.find(function(input) {
return input.name == 'content';
}).value = content_val;
console.log(values);
Run Code Online (Sandbox Code Playgroud)