本文转自:http://forum.mootools.net/viewtopic.php?id=4765
原文如下:
if you are trying to send htmls elements arrays to json, you'll get problem. json.toQuery does not work with forms, and form.toQueryString is disigned to work with ajax, not json.
so i spend some time to extend elements class to this :
Element.extend({
toJsonString:function(){
var formElements = {};
var j=0;
this.getFormElements().each(function(el){
var name = el.name;
var value = el.getValue();
if (value === false || !name || el.disabled) return;
if (formElements[name.replace('[]', "")])
formElements[name.replace('[]', "")].push(value);
else formElements[name.replace('[]', "")] = [value];
});
for (var element in formElements){
if (formElements[element].length == 1)
formElements[element] = formElements[element][0];
}
return Json.toString(formElements);
}
});
|
at least with php, this function do send vars and arrays through json to php. all you have to do is
var jSonRequest = new Ajax('my_json_data.php', {
data: $('myform').toJsonString(),
onComplete: my_oncomplete
});
jSonRequest.request();
|
and you'll have your form ready to be send.