我试图用jQuery的$.ajax()函数提交一个表单,它工作,除了它只发送最后几个复选框的值,不管我检查了多少。如果有人能告诉我我做错了什么,我会很感激的。提前感谢!
HTML表单:
<form name="contactFormSmall" id="contactFormSmall" action="" method="post">
<input type="text" name="textfieldname" value="textfieldvalue" />
<input type="checkbox" name="checkboxes" value="Testvalue One" checked="checked" />
<input type="checkbox" name="checkboxes" value="Testvalue Two" checked="checked" />
<input type="checkbox" name="checkboxes" value="Testvalue Three" checked="checked" />
</form>
下面是jQuery处理表单的代码:
var fields = $(":input").serializeArray();
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
url: "includes/contact_process_small.php",
data: fields,
success: function(data) {
$('#contactFormSmall').hide();
$("#formFeedback").html(data).show();
}
});
下面是php脚本中print_r($_POST)的结果:
Array
(
[textfieldname] => textfieldvalue
[checkboxes] => Testvalue Three
)
name="checkboxes"
仅在单个元素上是正确的,您必须使用name="checkboxes[]"
更好的序列化表单方法
像这样
数据:$ (' # contactFormSmall ') .serialize (),
并改为name="checkboxes[]
try this:
$('form#contactFormSmall').serializeArray();
name属性是POST数据的键值对中的键。因此,使用name="checkboxes[]"创建数组或使用:
<form name="contactFormSmall" id="contactFormSmall" action="" method="post">
<input type="text" name="textfieldname" value="textfieldvalue" />
<input type="checkbox" name="checkboxA" value="Testvalue One" checked="checked" />
<input type="checkbox" name="checkboxB" value="Testvalue Two" checked="checked" />
<input type="checkbox" name="checkboxC" value="Testvalue Three" checked="checked" />
</form>
分别获取每个复选框的值(这更可能是您想要的)。