[英]How do I change serializeObject output format?
I'm quite new to javascript and can't find the solution. 我对javascript很陌生,找不到解决方案。 I have the following code. 我有以下代码。
<form class="edit-task-form">
<legend>Create Task</legend>
<label>Task</label>
<input type="text" name="task" />
<hr />
<button type="submit" class="btn">Create</button>
</form>
var taskDetails = $(ev.currentTarget).serializeObject();
var task = new Task;
task.save(taskDetails, {
success: function(task) {
alert(task.toJSON());
}
});
console.log(taskDetails);
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if(o[this.name] !==undefined) {
if(!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
}
else {
o[this.name] = this.value || '';
}
});
return o;
The output is: Object {task: "fasdfasd"} 输出为:对象{task:“ fasdfasd”}
I would like it to be: Object {"task": "fasdfasd"} 我希望它是:Object {“ task”:“ fasdfasd”}
How may I proceed to have "task" to appear in quotation marks? 如何使“任务”出现在引号中?
Thanks a lot! 非常感谢!
David 大卫
One solution : 一种解决方案:
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if(o[this.name] !==undefined) {
if(!o[this.name].push) {
o[this.name] = ['"' + o[this.name] + '"' ]; //<---------
}
o[this.name].push(this.value || '');
}
else {
o[this.name] = this.value || '';
}
});
return o;
}
$.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if(o[this.name] !==undefined) { if(!o[this.name].push) { o[this.name] = ['"' + o[this.name] + '"' ]; //<--------- } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; } var val = $('.edit-task-form').serializeObject(); document.write('<pre>'+ JSON.stringify( val , null , ' ') + '</pre>') $('form').submit(function(evt){ evt.preventDefault(); document.write('<pre>'+ JSON.stringify( $(this).serializeArray() , null , ' ') + '</pre>') });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="edit-task-form"> <legend>Create Task</legend> <label>Task</label> <input type="text" name="task" /> <hr /> <button type="submit" class="btn">Create</button> </form>
You have this function: 您具有以下功能:
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if(o[this.name] !==undefined) {
if(!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
}
else {
o[this.name] = this.value || '';
}
});
return o;
you need to change it to this: 您需要将其更改为此:
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if(o['"'+this.name+'"'] !==undefined) {
if(!o['"'+this.name+'"'].push) {
o['"'+this.name+'"'] = [o[this.name]];
}
o['"'+this.name+'"'].push(this.value || '');
}
else {
o['"'+this.name+'"'] = this.value || '';
}
});
return o;
Just add to the key the quotes 只需在引号中添加引号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.