在我的ajax表单中,我有一些复选框和一个电子邮件地址文本框。表单有效,但我无法在同一变量中发送复选框数组和电子邮件地址。对不起,我的英语不好。这是jquery代码:
$(document).ready(function () {
$('#submit').click(function () {
var valore = { 'how_hear[]' : []};
$("input:checkbox:checked").each(function() {
valore['how_hear[]'].push($(this).val());
}); // Checkboxes value
var name = $('input:text');
组织要发送到 php 的数据。我认为问题就在这里,但我无法解决!!
var data = 'name=' + name.val() + '&check=' valori;
$.ajax({
type:'POST',
url:'myurl.php',
data: data, // If i send only the variable "name" or only the variable "valore", it works fine!
cache: false,
success:function (data) {
$('form').append('GIUSTO!!!!');
}
});
return false;
});
});
有什么建议吗?谢谢!
您可以使用
serializeArray()
或serialize()
方法。
将数据发送到服务器
http://api.jquery.com/jQuery.ajax/
默认情况下,Ajax 请求使用 GET HTTP 方法发送。如果需要 POST 方法,可以通过设置类型选项的值来指定该方法。此选项会影响将数据选项的内容发送到服务器的方式。根据 W3C XMLHTTPRequest 标准,POST 数据将始终使用 UTF-8 字符集传输到服务器。
The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}
.如果使用后一种形式,则在发送数据之前,将使用 jQuery.param() 将数据转换为查询字符串。可以通过将 processData 设置为 false 来规避此处理。如果您希望将 XML 对象发送到服务器,则可能需要处理;在这种情况下,将 contentType 选项从 application/x-www-form-urlencoding 更改为更合适的 MIME 类型。