简体   繁体   English

将JavaScript变量传递到Ajax数据中:

[英]Passing JavaScript Variable into Ajax data:

I am working on a jQuery sortable. 我正在对jQuery进行排序。 I pull information from my database to populate each orderable element and then repost the new order back to my data base. 我从数据库中提取信息以填充每个可订购元素,然后将新订单重新发布回我的数据库。 However I cannot figure out how to take the variable that I define in my serialize jQuery and enter it into my AJAX function in the data: area. 但是,我无法弄清楚如何采用我在序列化jQuery中定义的变量并将其输入到data:区域的AJAX函数中。

Here is my code: 这是我的代码:

$(document).ready(function() 
{              
    $('#social_list').sortable(
    { 
        update: function() 
        {
            var order = $('#social_list').sortable('serialize');
            alert(order);
        }                         
    });
});

$.ajax(
{
    url: 'save_items_order.php',
    data: ?,
    type: '$_POST', 
    success: function(msg)
    {
        alert(msg);
    }
});

So I want to take the string that var order creates and pass it into the ajax so data: = var order string 所以我想采用var order创建的字符串并将其传递到ajax中,这样data:= var order string

I tried data: $('#social_list').sortable('serialize'), everything seemed to work it just doesn't seem to update my table 我尝试了数据:$('#social_list')。sortable('serialize'),似乎一切正常,但似乎没有更新我的表

Simplest solution is to make a global 最简单的解决方案是

$(document).ready(function() {
    $('#social_list').sortable({
        update: function() {
            // global. you could just omit var, but I find this clearer
            window.order = $('#social_list').sortable('serialize');
        }
    });
});

$.ajax({
    url: 'save_items_order.php',
    data: window.order, /* ...rest of code */
});

But I'm sure you know globals are bad, you can at least contain them to your namespace, 但我敢肯定,您知道全局变量很糟糕,您至少可以将它们包含在名称空间中,

var myNs = {};
$(document).ready(function() {
    $('#social_list').sortable({
        update: function() {
            myNs.order = $('#social_list').sortable('serialize');
        }
    });
});

$.ajax({
    url: 'save_items_order.php',
    data: myNs.order /* ... rest of code */
});

or re-organize your code so they can share variables. 或重新组织您的代码,以便他们可以共享变量。

$(document).ready(function() {
    var order;
    $('#social_list').sortable({
        update: function() {
            order = $('#social_list').sortable('serialize');
        }
    });
    $('#myButton').click(function(){
        $.ajax({
            url: 'save_items_order.php',
            data: order, /* ... rest of code */
        });
    });
});

Note that for this case, you could just do the following 请注意,在这种情况下,您可以执行以下操作

$.ajax({
    url: 'save_items_order.php',
    data: $('#social_list').sortable('serialize') /* ... rest of code */
});

You just pass any javascript object in as the data. 您只需将任何javascript对象作为数据传入即可。

For your specific case, I would probably wrap the AJAX call in a function, like: 对于您的特定情况,我可能会将AJAX调用包装在一个函数中,例如:

function saveItemOrder(order)
{
    $.ajax({
        url: 'save_items_order.php',
        data: { "order": order },
        type: 'POST', // <-- this should be just "POST"
        success: function(msg){
            alert(msg);
        }
    });
}

And then: 接着:

update: function() {
    var order = $('#social_list').sortable('serialize');
    // alert(order);

    saveItemOrder(order);
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM