我有一个简单的电子邮件联系表单,并且有多个输入字段。现在,它只发送其中一个字段(消息)的值。我如何修改代码,以便我可以从字段中发送所有信息?我有大约15个领域。
以下是代码:
$('#submit').click(function(){
$.ajax({
type: "POST",
url: "mail.php",
data: ({email : $("#email").val(), message: $("#message").val()}),
cache: false,
error: function () {
alert('did not go thru');
},
success: function(html){
//$("#response").fadeIn("slow");
$("#tab1").html(html);
//setTimeout('$("#response").fadeOut("slow")',2000);
alert('mail sent');
}
});
});
答案 0 :(得分:4)
如果你给表单一个id,并将回调移动到表单的提交事件,你可以很容易地使用serialize方法自动发送整个表单:
$('#form-id').submit(function(){
$.ajax({
type: "POST",
url: "mail.php",
data: $(this).serialize(),
cache: false,
error: function () {
alert('did not go thru');
},
success: function(html){
//$("#response").fadeIn("slow");
$("#tab1").html(html);
//setTimeout('$("#response").fadeOut("slow")',2000);
alert('mail sent');
}
});
});
答案 1 :(得分:0)
为什么不将剩余的字段添加到数据中?