另一种管理表单所有字段的解决方案。

form.html 强>

Document

....

opt1

opt2

opt3

opt4

test content!!

checkbox 1

Apple

Orange

procesa.php 强>

$uno = $_POST['uno'];

var_dump($_POST);

process.js 强>

$(function() {

$( "#myUno" ).submit(function( event ) {

event.preventDefault();//

//we get the data in a single table through our function below

var formData = $('#myUno').serializeAssoc();

console.log(formData);

$.ajax({

type: "POST",

url: "procesa.php",

data: { uno: formData.uno }

})

.done(function( data ) {

$('#result').html(data); //displays the response returned. This is used to debug if necessary

});

});

});

$.fn.serializeAssoc = function() {

var data = {};

$.each( this.serializeArray(), function( key, obj ) {

var a = obj.name.match(/(.*?)\[(.*?)\]/);

if(a !== null)

{

var subName = a[1];

var subKey = a[2];

if( !data[subName] ) data[subName] = [ ];

if( data[subName][subKey] ) {

if( $.isArray( data[subName][subKey] ) ) {

data[subName][subKey].push( obj.value );

} else {

data[subName][subKey] = [ ];

data[subName][subKey].push( obj.value );

}

} else {

data[subName][subKey] = obj.value;

}

} else {

if( data[obj.name] ) {

if( $.isArray( data[obj.name] ) ) {

data[obj.name].push( obj.value );

} else {

data[obj.name] = [ ];

data[obj.name].push( obj.value );

}

} else {

data[obj.name] = obj.value;

}

}

});

return data;

};

我发现此插件$。fn.serializeAssoc效果良好here

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐