Php jquery序列化不处理复选框
我在一些复选框上有一个jquery,我正在尝试序列化这些值,以便在PHP中获得多个选择 以下是我的jquery:Php jquery序列化不处理复选框,php,jquery,serialization,checkbox,Php,Jquery,Serialization,Checkbox,我在一些复选框上有一个jquery,我正在尝试序列化这些值,以便在PHP中获得多个选择 以下是我的jquery: <script type="text/javascript"> jQuery(document).ready(function($) { $("input:checkbox").change(function() { if($(this).is(':checked')) {
<script type="text/javascript">
jQuery(document).ready(function($) {
$("input:checkbox").change(function() {
if($(this).is(':checked'))
{
var color = $(this).val().serialize;
$(".itemMain").hide();
$(".indexMain").load('indexMain.php?color='+color);
}
});
});
</script>
在应用th serialize之前,一切正常,但我不能有多个选择。现在,serialize()根本不起作用
有什么建议吗?谢谢 粗略的解决方案可以是,单击每个复选框的事件时,您可以以逗号分隔的形式将其值存储/附加到变量(或隐藏的输入字段)中,并将此变量传递给颜色查询字符串。另一个对我有效的解决方案 只需在字段的name属性中指定要在其他术语中使用多个值:数组
<form action="" method="post" id="filters_form"">
<input id="fieldNameText" value="value0" name="fieldNameText" type="text">
<input type="checkbox" name="myFieldName[]" id="myFieldName" value="value1"/>
<input type="checkbox" name="myFieldName[]" id="myFieldName" value="value2"/>
</form>
如果您使用AJAX在post中发送这些数据,您将在php中看到:
阿贾克斯
PHP
由于$(this).val()将始终返回一个值,我不明白为什么需要序列化?无法序列化复选框的值,因为返回的值不是jQuery对象。另外,您实际上没有调用
serialize()
,因为您忘记了括号。如果选中多个框,我实际上需要序列化。我该怎么做?
<form action="" method="post" id="filters_form"">
<input id="fieldNameText" value="value0" name="fieldNameText" type="text">
<input type="checkbox" name="myFieldName[]" id="myFieldName" value="value1"/>
<input type="checkbox" name="myFieldName[]" id="myFieldName" value="value2"/>
</form>
fieldNameText=value0&myFieldName%5B%5D=value1&myFieldName%5B%5D=value2
$.ajax({
type: "POST",
url: "yourFormURL",
data: dataString,
success: function(response) {
//do something
}
});
print_r($_POST);
/* Output :
Array ( [fieldNameText] => value0 [myFieldName] => Array ( [0] => value1 [1] => value2 ))
*/