如何通过jQuery Ajax调用传递表单数据和我自己的变量? [英] How can I pass form data AND my own variables via jQuery Ajax call?

查看:102
本文介绍了如何通过jQuery Ajax调用传递表单数据和我自己的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的工作代码:

jQuery.ajax({
type: 'post',
url: ajaxurl,
dataType: 'json',
data: jQuery('select[name^="option"], :input[name^="option"]),
success: function (mydata) {
    // do something
}
});

我想添加一个变量,并将其与称为"myVar"的select和textbox选项值一起传递回去.但是添加时在参数中看不到它:

I want to add a variable to be passed back along with the select and textbox option values called 'myVar'. But I don't see it in the parameters when I add it:

var myVar = '123';

jQuery.ajax({
type: 'post',
url: ajaxurl,
dataType: 'json',
data: jQuery('select[name^="option"], :input[name^="option"], myvar='+myVar),
success: function (mydata) {
    // do something
}
});

我做错什么了吗?我是否需要序列化或编码URIcomponent或其他内容?似乎没有任何影响.我仍然可以获得select和textbox数据,但是myVar根本不在$ _POST端.

Am I doing something wrong? Do I need to serialize or encodeURIcomponent or something? Nothing seems to affect it. I still get the select and textbox data, but myVar doesn't come at all in the $_POST side.

有想法吗?

推荐答案

您是否尝试过:

 var myVar = '123';
 jQuery.ajax({
    type: 'post',
    url: ajaxurl,
    dataType: 'json',
    data: jQuery.param(jQuery('select[name^="option"]').val()) + jQuery.param(jQuery('input[name^="option"]').val()) + '&myvar=' + myVar,
    success: function (mydata) {
        // do something
    }
  });

我认为您也可以使用traditional: true代替param

I think you can also use traditional: true instead of param

这篇关于如何通过jQuery Ajax调用传递表单数据和我自己的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆