开发者

jQuery Post: Appending data to post

I'm using jQuery's post on a WordPress page.

var thename = jQuery("input#name").val();
    jQuery.post(the_ajax_script.ajaxurl开发者_如何转开发, jQuery("#theForm").serialize(),
    function(response_from_the_action_function){
    jQuery("#response_area").html(response_from_the_action_function);
});

It posts the selections made in a form. Is it possible to append data to a post. So in addition to the form data, I need a couple lat longs added to the jQuery post. How can I do that. Is it possible?

Thnak you.

-Laxmidi


.post() takes a string as the second argument, so you can concatenate a custom string using + followed by your string.

var thename = jQuery("input#name").val();
    jQuery.post(the_ajax_script.ajaxurl, jQuery("#theForm").serialize() + "&foo=bar",
    function(response_from_the_action_function){
    jQuery("#response_area").html(response_from_the_action_function);
});

Do be careful, though; the string has to be correctly formatted as a URL, so you need key=value pairs separated by &s.

In the example above, you'll see + "&foo=bar. The first & finishes the last value created by .serialize(), then foo= is a key, followed by bar as the value.

If you want to add more values afterwards, you can do something like this:

&foo=bar&baz=zip


var thename = jQuery("input#name").val();
var data = {'anycolortoulike':'transparent'}; // any data;
jQuery.post(the_ajax_script.ajaxurl, data.concat(jQuery("#theForm").serializeArray()),
    function(response_from_the_action_function){
       jQuery("#response_area").html(response_from_the_action_function);
    }
);

try this, $.post can use arrays

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜