我有一个 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.....
我想通过 mootools ajax 发送(使用 post 方法)选定的类别(category[]),这样如果我将 $_POST 变量转储到服务器中,我会得到类似的内容:
array(1) {
[category]=>
array(2) {
[0]=>
string(4) "Math"
[1]=>
string(7) "History"
}
}
它的 javascript(mootools) 代码应该是什么?下面是我的部分代码。
new Request.JSON({url: '/ajax_url',
onSuccess: function(){
alert('success');
}
}).post(???);
请注意,我不想发送名字和姓氏字段。我只想发送类别字段,它是一个 html 数组。
最佳答案
您只需序列化字段即可:
var theForm = $('form-id');
new Request.JSON({url: '/ajax_url',
onSuccess: function(){
alert('success');
}
}).post(theForm.toQueryString()); // this will create the GET-style query
关于javascript - 使用 Request.JSON 将 html 数组作为 post 变量发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2854176/