我有两个不同的数组,即main_name []和sub_name []。 我必须使用AJAX将这些值传递到下一页,以便插入到DB中。请你帮帮我,我怎样才能将数组作为数据传递给AJAX。 HTML
<input type="text" name = "main_name[]" value = "" >
<input type="text" name = "sub_name[]" value = "" >
JS
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});
答案 0 :(得分:2)
$('form').serialize()
会将所有表单元素与值序列化,并与ajax查询一起发送。
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: $('#form_id').serialize(),
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});
答案 1 :(得分:2)
您只需使用serializeArray
即可var data = $(form).serializeArray();
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function (data) {
$(".the-return").html();
}
});
答案 2 :(得分:1)
尝试此操作,它将在(数据)变量中获取表单的所有值,并通过json格式的ajax发送。
var data = $('form').serialize();
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});