0

I am using jQuery ajax method to insert the data to database.
Able to insert the data successfully, and returning the newly inserted id.

Here the problem is, I am appending the new row immediately after submit (prior to insertion of database)

Keeping in mind that, the data will be inserted from background, without causing any delay to the user.
He must be able to keep adding rows, and they must be saved to DB in background.

$('#add-new-row').live('submit', function(){        
   var obj = $(this),
   newdata = obj.find('.newdata');
   $('#target').append('<div>newdata.val()+'</div>');

   $.ajax({
       url: '/insert/insertrow',
       data: $(this).serialize(),
       dataType: 'json'
       // other settings
       success: function(response) {
           alert(response.id); // response contains the newly inserted id
       }
   });

}

But I need to attach the ID of new row to html for further processing.
Hence I am struck, to add this response.id to new row as one attribute.

In the above jQuery event I am appending the new row to the target as

<div>New row</div>

It should become something like this after it got inserted to database.

<div rowid="123">New row</div>

Any suggestions to achieve this scenario.

4

1 回答 1

2

How about putting this in the success:

 $("#target div:last-child").attr("rowid", response.id) 
于 2013-03-30T14:23:01.387 回答