[英]Having problems with showing/hiding form using button (PHP/HTML/Javascript)
[英]Problems with html/php Form to JSON
我意识到我可能应该为此使用角度,但是我不能在该项目中这样做。
我想做的就是轻松地将表单转换为javascript对象,该对象将通过AJAX传递。
然后,PHP脚本应该简单地处理对象并用它来处理我需要的事情。
问题是我的对象格式不正确。
因此,具有如下字段的表单:
<input type="text" name="chart_data[1][value]" />
但是无论出于什么原因,我的对象看起来都是这样!
chart_data[1: Object
color: "#726d86"
highlight: "#4f4868"
label: "Transportation"
value: "10"
右括号不见了! chart_data [1
我尝试了两种不同的功能来尝试将数据转换为对象。 两者似乎都产生相同的问题。
Javascript仍然是我最弱的语言,因此任何帮助将不胜感激!
(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;
};
})(jQuery);
/*!
* jQuery serializeObject - v0.2 - 1/20/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// Whereas .serializeArray() serializes a form into an array, .serializeObject()
// serializes a form into an (arguably more useful) object.
(function($,undefined){
'$:nomunge'; // Used by YUI compressor.
$.fn.serializeObject = function(){
var obj = {};
$.each( this.serializeArray(), function(i,o){
var n = o.name,
v = o.value;
obj[n] = obj[n] === undefined ? v
: $.isArray( obj[n] ) ? obj[n].concat( v )
: [ obj[n], v ];
});
return obj;
};
})(jQuery);
您无需执行任何操作。 jQuery内置的serialize()
将为您处理
$('#formID').on('submit',function(e){
e.preventDefault();
$.post(url, $(this).serialize(), function(resp){
// do something with response
}[,dataType])
});
然后在php中,可以使用以下命令提供由相关input
表示的数组:
$chartDataArray = $_POST['chart_data'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.