Javascript 与jQuery中的序列化数组相反(还原形式)
如果数据来自Javascript 与jQuery中的序列化数组相反(还原形式),javascript,jquery,html,forms,serialization,Javascript,Jquery,Html,Forms,Serialization,如果数据来自序列化数组,如何使用它更新表单 var values = form.serializeArray(); form.deserializeArray(value); // What is the deserializeArray analogue? form.seriaizeArray() === values; // So that this is always true 请参见序列化数组的反面是.param() 实际上,serialize函数首先执行serializeArray,
序列化数组
,如何使用它更新表单
var values = form.serializeArray();
form.deserializeArray(value); // What is the deserializeArray analogue?
form.seriaizeArray() === values; // So that this is always true
请参见序列化数组的反面是.param() 实际上,serialize函数首先执行serializeArray,然后应用param()
我们可以迭代数组并恢复表单
for (var i = 0; i < values.length; i++) {
$("input[name='" + values[i].name + "'], select[name='" + values[i].name + "']").val(values[i].value);
}
for(变量i=0;i
这是我的工作
// By Jorhel Reyes
jQuery.fn.deserializeArray = function (ObjectSerialized, isJson)
{
var $form = jQuery(this);
var json = {};
jQuery.each(ObjectSerialized, function(i, pair){
var name = decodeURIComponent(pair.name).split("[");
if( typeof name[1] != "undefined" ){
if( typeof json[name[0]] === "undefined"){ json[name[0]] = []; }
json[name[0]].push(decodeURIComponent(pair.value));
}else{
json[name[0]] = decodeURIComponent(pair.value);
}
});
var asignValue = function(element, val){
if( !element.length ){ return; }
if (element[0].type == "radio" || element[0].type == "checkbox") {
var $fieldWithValue = element.filter('[value="' + val + '"]');
var isFound = ($fieldWithValue.length > 0);
// Special case if the value is not defined; value will be "on"
if (!isFound && val == "on") {
element.first().prop("checked", true);
} else {
$fieldWithValue.prop("checked", isFound);
}
} else { // input, textarea
element.val(val);
}
};
jQuery.each(json, function(name, value){
var element = '';
if( typeof value === "object" ){
element = $form.find('[name="' + name + '[]"]');
jQuery.each(value, function(k, val){
var elm = jQuery( element[k] );
asignValue(elm, val);
});
}else{
asignValue($form.find('[name="' + name + '"]'), value);
}
});
return this;
}
是的,这和我现在做的差不多。我想知道是否有一种官方的方式可以从jQuery本身(或现有的和维护的插件)来实现它。这种方式只适用于“文本、文本区域、单选”而不是“单选、复选框、多选”如果您提供一个简短的解释来伴随您的代码会很有帮助。只有代码的答案通常是低质量的答案,没有任何解释。
// By Jorhel Reyes
jQuery.fn.deserializeArray = function (ObjectSerialized, isJson)
{
var $form = jQuery(this);
var json = {};
jQuery.each(ObjectSerialized, function(i, pair){
var name = decodeURIComponent(pair.name).split("[");
if( typeof name[1] != "undefined" ){
if( typeof json[name[0]] === "undefined"){ json[name[0]] = []; }
json[name[0]].push(decodeURIComponent(pair.value));
}else{
json[name[0]] = decodeURIComponent(pair.value);
}
});
var asignValue = function(element, val){
if( !element.length ){ return; }
if (element[0].type == "radio" || element[0].type == "checkbox") {
var $fieldWithValue = element.filter('[value="' + val + '"]');
var isFound = ($fieldWithValue.length > 0);
// Special case if the value is not defined; value will be "on"
if (!isFound && val == "on") {
element.first().prop("checked", true);
} else {
$fieldWithValue.prop("checked", isFound);
}
} else { // input, textarea
element.val(val);
}
};
jQuery.each(json, function(name, value){
var element = '';
if( typeof value === "object" ){
element = $form.find('[name="' + name + '[]"]');
jQuery.each(value, function(k, val){
var elm = jQuery( element[k] );
asignValue(elm, val);
});
}else{
asignValue($form.find('[name="' + name + '"]'), value);
}
});
return this;
}