Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Request.JSON将html数组作为post变量发送_Javascript_Ajax_Arrays_Json_Mootools - Fatal编程技术网

Javascript 使用Request.JSON将html数组作为post变量发送

Javascript 使用Request.JSON将html数组作为post变量发送,javascript,ajax,arrays,json,mootools,Javascript,Ajax,Arrays,Json,Mootools,我有一个html: First name: <input type='text' name='first_name' value='' /><br/> Last name: <input type='text' name='last_name' value='' /><br/> <input type='checkbox' name='category[]' value='Math' /> Math<br/> <inp

我有一个html:

First name: <input type='text' name='first_name' value='' /><br/>
Last name: <input type='text' name='last_name' value='' /><br/>
<input type='checkbox' name='category[]' value='Math' /> Math<br/>
<input type='checkbox' name='category[]' value='Science' /> Science<br/>
<input type='checkbox' name='category[]' value='History' /> History<br/>
etc.....
它的javascript(mootools)代码应该是什么?下面是我的部分代码

new Request.JSON({url: '/ajax_url',
            onSuccess: function(){
                alert('success');
            }
        }).post(???);

请注意,我不想发送first_name和last_name字段。我只想发送category字段,它是一个html数组。

我会检查Prototype JS库中该函数的源代码,看看它是如何工作的:第100行


接受
类别[]
格式可能需要一些调整,但这是一个开始,或者是一个灵感。

您只需序列化字段:

var theForm = $('form-id');

new Request.JSON({url: '/ajax_url',
    onSuccess: function(){
        alert('success');
    }
}).post(theForm.toQueryString()); // this will create the GET-style query

这里有一种方法:

$("formid").addEvent("submit", function(e) {
    e.stop();
    var x = {};
    this.toQueryString().split('&').each(function(i) {
        var p = i.split('=');
        if (p[0].contains("[]")) {
            p[0] = p[0].replace("[]", "");
            if ($type(x[p[0]]) != "array")
                x[p[0]] = [];
            x[p[0]].push(p[1]);
        }
        else {
            x[p[0]] = p[1];
        }

    });
    // return a number of formats, depends on your backend:
    console.log(x, JSON.encode(x), $H(x).toQueryString());
});
这可以输出:

对象(x)或

json
{“名字”:“Dimitar”,“姓氏”:“Christoff”,“category”:[“Math”,“Science”]}

查询字符串:
first\u name=Dimitar&last\u name=Christoff&category[0]=数学&category[1]=科学

祝你好运。案例:

p、 您不应该使用
name[]
作为字段名,这确实是PHP处理事情的一种快捷方式/好方法,但不是客户端imo

$("formid").addEvent("submit", function(e) {
    e.stop();
    var x = {};
    this.toQueryString().split('&').each(function(i) {
        var p = i.split('=');
        if (p[0].contains("[]")) {
            p[0] = p[0].replace("[]", "");
            if ($type(x[p[0]]) != "array")
                x[p[0]] = [];
            x[p[0]].push(p[1]);
        }
        else {
            x[p[0]] = p[1];
        }

    });
    // return a number of formats, depends on your backend:
    console.log(x, JSON.encode(x), $H(x).toQueryString());
});