0

我正在通过 Jquery Ajax 帖子发送数据,我想从我的表单中发送文本框,还有一个名为 username 的变量。例如...

$.ajax({type:'POST', url: 'submit.php', data:$('#myform').serialize(), username});

我怎样才能做到这一点?

4

6 回答 6

1

只需将额外参数添加到序列化字符串

$.ajax({
    type:'POST', 
    url: 'submit.php', 
    data:$('#myform').serialize()+'&username='+encodeURIComponent(username)
});
于 2013-01-11T19:44:15.813 回答
1

As a good practice, it is better to construct a data object and also include the extra variables. Try constructing a data object and include them ?

data = {
    'formData': $('#myform').serialize(),
    'variable': 'username'
}
于 2013-01-11T19:35:58.277 回答
0

You can pass it your other variable via get like this.

$.ajax({type:'POST', url: 'submit.php?myvar='+encodeURIComponent(myvar), data:$('#myform').serialize(), username});

You could also add the value to a hidden field in your form.

于 2013-01-11T19:35:22.757 回答
0

尝试这个

   $.ajax({ 
         type:'POST', 
         url: 'submit.php', 
         data: { form: $('#myform').serialize(), username: encodeURIComponent(username)}
    });

检查ajax 文档将数据发送到服务器的部分

于 2013-01-11T19:56:38.790 回答
0

试试下面的查询。用户名应该是一个 js 变量。

var username = "myname";

$.ajax({
 type:'POST', 
 url: 'submit.php', 
 data:$('#myform').serialize(), 
 username:username
});
于 2013-01-11T19:34:44.943 回答
0

尝试,

$.ajax({ 
  type:'POST', 
  url: 'submit.php', 
  data:$('#myform').serialize(), 
  username: username //you missed the key
});
于 2013-01-11T19:34:58.327 回答