如何在jquery ajax调用中动态传递数据参数(多个数据数组) [英] How to dynamically pass data parameter in a jquery ajax call (multiple arrays of data)

查看:944
本文介绍了如何在jquery ajax调用中动态传递数据参数(多个数据数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个ajax代码:

I have this ajax code:

return $.ajax({
        type: "POST",
        url: "somefile.php",
        cache:false,
        data: { "yfilter": $("#yearFilter").serializeArray(),
            "gfilter": $("#genreFilter").serializeArray() },
        dataType:"json",
        success: function(data){
            alert("success");
        }

这很好,但是我需要动态传递data参数.现在,我需要上面的数据参数内容和一个字符串.

This works fine, but I need to pass the data parameter dynamically. For now I need the above data parameter content and a single string.

如何动态传递此信息? /如何将其存储在变量中并将其传递给数据:"字段?

How do I pass this dynamically? / How do I store it in a variable and pass it to the "data:" field?

 { "yfilter": $("#yearFilter").serializeArray(),
    "gfilter": $("#genreFilter").serializeArray() }

我尝试了JSON.stringify,但由于数据是数组,我无法使其正常工作.

I tried JSON.stringify I I couldn't get it to work probably due to the data being an array.

year和genre数组直接来自jquery下拉菜单.就像它的#id一样被选中,例如"$(#yearFilter").这是选择表单元素.

The year and genre arrays are coming directly from the jquery drop down menu. It is selected like by it's #id like so "$("#yearFilter")". This is the select form element.

<select multiple="multiple" name="yearFilter[]" class="filterChange" id="yearFilter">

我在基本级别上需要的是:

var someData = "";


    if(...){
        someData = { "yfilter": $("#yearFilter").serializeArray(),
                "gfilter": $("#genreFilter").serializeArray() };
    }
    else if(...){
        someData = "sampleString";
    }

在ajax调用中:

...
data: someData,
...

推荐答案

我想我有一个想要的主意,但是发布由于诸如json stringify之类的多余问题而变得过于复杂.这是一个可以在代码中的多个地方使用的函数,可以进行一种类型的AJAX调用或另一种类型.

I think I have an idea what you want but post has been overly complicated by extraneous issues like json stringify . Here's a function that could be used in several places eleswhere in your code to make one type of AJAX call or another.

然后您可能会有几个按钮,并在处理程序中为每种按钮类型调用函数,并更改传递给函数的参数

You would then perhaps have several buttons and call the function within handlers for each type of button and change the argument passed to function

doAjax('someval');/* use in button 1*/
doAjax('someOtherval');/* use in button 2*/



function doAjax(arg) {

    var someData = "";
    if (arg == 'someval') {
        someData = {
            "yfilter": $("#yearFilter").val(),
            "gfilter": $("#genreFilter").val()
        };
    } else {
        someData = "sampleString";
    }

    $.ajax({
        type: "POST",
        url: "somefile.php",
        cache: false,
        data: someData,
        dataType: "json",
        success: function(data) {
            if (arg == 'someval') {
                alert("success 1");
            } else {
                alert("success 2");
            }
        }
    })
}

这篇关于如何在jquery ajax调用中动态传递数据参数(多个数据数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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