views:

40

answers:

3

The following snippet runs through a bunch of form elements, grabs the name and values and alerts them. If there are 3 inputs, I get three alerts. I need to combine these all so I can submit them via .post but not sure how to do that.

I can't use .serializeArray as I don't have a form tag I can use. Backend is .net and there can be up to 20 different forms on the page so I can't submit the whole thing.

Can anyone point me in the right direction?

$('.savefunctions a').live('click', function() { 
    var fields = $(this).parents('.ui-accordion-content').find(':input');
            $.each(fields, function(i, field) {
        alert(field.name+': "'+field.value+'", ');
    });
});
+3  A: 

You don't need a <form> to use .serializeArray().

From the docs:

This method can act on a jQuery object that has selected individual form elements, such as <input>, <textarea>, and <select>.

var result = $(this).parents('.ui-accordion-content')
                                          .find(':input').serializeArray();
patrick dw
Thanks for the reply. From there, I've now managed to get it posting back as json and all is well in the world. :)
Trip
A: 

Use an object:

$('.savefunctions a').live('click', function() { 
    var o = {};
    var fields = $(this).parents('.ui-accordion-content').find(':input');
    $.each(fields, function(i, field) {
        o[field.name] = field.value;
    });
   $.post('http://my.url/', o);
});
pygorex1
A: 

Try this:

$(fields).each( <do your processing here for each field> );

Note: Inside the .each(), to iterate through each field use the keyword this. But if you need to apply jQuery functions then do something like this:

$(this).click();
Sidharth Panwar