简体   繁体   English

ajax并在jquery中添加serializearray

[英]ajax and adding the the serializearray in jquery

How do I make this work? 我该如何工作? I'm trying to do AJAX post. 我正在尝试发布AJAX。 i'm use to doing .serialize but i want to add two more values and keys to the array. 我习惯做.serialize,但我想向数组添加两个以上的值和键。 how can I do this easily? 我如何轻松做到这一点?

        $('#moreprojects').click(function(){
            var skip = $(this).attr('name');
            var more = $(this).attr('rel');
            var param = $('#companies').serializeArray();
            param.push({name: 'skip', value: skip})
            param.push({name: 'more', value: more})
            $.post('projectsmore.php', {param}, function(response){
                $('#projects tbody').append(response);
            })  
        })

The way you add the values should be fine. 您添加值的方式应该很好。 But your call to $.post should be: 但是您对$.post的调用应该是:

$.post('projectsmore.php', param, function(...

(no {} around param ). (在param周围没有{} )。

You're experiencing problems because of the way that you're injecting the param variable into the $.post. 由于将param变量注入$ .post中的方式,您遇到了问题。 Because the variable param is already an object you don't need to wrap it with brackets. 因为变量param已经是一个对象,所以不需要用方括号将其包装起来。

So instead of: 所以代替:

$.post('projectsmore.php', {param},

it should be: 它应该是:

$.post('projectsmore.php', param, 

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

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