views:

27

answers:

3

I'm got a form laid out like a spreadsheet. When the user leaves a row, I want to submit fields from that row to the server using jQuery Ajax. The page is one large form, so this isn't really a javascript clicking the submit button scenario - the form is huge and I only want to send a small portion of the content for reasons of speed.

I've got the code written that identifies the row and iterates through the fields in the row. My issue is how to build the dat object in order to submit something comprehensible I can disassemble and store at the server end.

At the moment my code looks like this

var dat=[];
$("#" + finalrow).find("input").each(function () {
    var o = $(this).attr("name");
    var v = $(this).val();
    dat.push({ o: v });
});
$.ajax({
    url: 'UpdateRowAjax',
    dataType: 'json',
    type: 'POST',
    data: dat ,
    success: function (data) {
        renderAjaxResponse(data);
    }
});

The assembling of dat doesn't work at all. So how should I build that dat object in order for it to "look" as much like a form submission as possible.

+1  A: 

You can add the elements that contain the data you want to send to a jQuery collection, and then call the serialize method on that object. It will return a parameter string that you can send off to the server.

var params = $("#" + finalrow).find("input").serialize();

$.ajax({
    url: 'UpdateRowAjax',
    type: 'POST',
    data: params ,
    success: function (data) {
        renderAjaxResponse(data);
    }
});
CD Sanchez
Thanks for your quick response. The jQuery site seems to be down and I've not used collection before (I assume its the plugin you are talking about) however, karim79's approach is all I need for this.
Andiih
@Andiih: All jQuery objects are actually collections. A jQuery object is a collection of zero or more elements. `$("#" + finalrow).find("input")` is a collection of elements that matched the selector and the additional `find` call. When you call serialize, it will a create a parameter string of all eligible elements in that collection. It's included in jQuery core, none of it requires a plugin.
CD Sanchez
@Daniel - thanks for the clarification that does in fact work perfectly!
Andiih
+1  A: 

$.ajax 'data' parameter expects a map of key/value pairs (or a string), rather than an array of objects. Try this:

var dat = {};
$("#" + finalrow).find("input").each(function () {
    dat[$(this).attr('name')] = $(this).val();
});
karim79
At first I thought this was working, but in fact although dat looks plausible once assembled, the servers formcollection is empty.
Andiih
+1  A: 

You can use $.param() to serialize a list of elements. For example, in your code:

var dat= $.param($("input", "#finalrow"));
$.ajax({
    url: 'UpdateRowAjax',
    dataType: 'json',
    type: 'POST',
    data: dat ,
    success: function (data) {
        renderAjaxResponse(data);
    }
});

Example of $.param(): http://jsfiddle.net/2nsTr/

serialize() maps to this function, so calling it this way should be slightly more efficient.

Andy E
Thanks. Very cool and a useful live example. I'm going to use karim79's solution as its conceptually simpler but I really value your input.
Andiih