0

I have form inputs in a table row, which I would like to submit via ajax.

$(this).closest("tr").find("input,select,textarea").each(function(){$data+=$(this).attr('name')+'='+encodeURIComponent($(this).val())+'&';

I had everything working fine - until one of the inputs had an ampersand in the value. I found some information on this post, which helped a lot - and using encodeURIComponent (as shown above) I was able to get the form to submit without a problem. My question is - in that post, @T.J. Crowder mentioned how he would "strongly recommend" using @Darin Dimitrov's solution:

data: { id: thisId, value: thisValue }

How would I implement this in my current code? Also - is there any benefit to using the second code, or is encodeURIComponent just as good?

Community
  • 1
  • 1
Alan
  • 389
  • 1
  • 7
  • 16

1 Answers1

1

You can use the native Javascript "Object" pseudo-class. I don't really understand your code's purpose; but, supossing it is correct, should look like this:

$data = new Object;
$(this).closest("tr").find("input,select,textarea").each(function(){
  $data[$(this).attr('name')] = $(this).val();
});

Hope this helps.

Yefb
  • 146
  • 8
  • This worked well. Do you know if there are any inherent benefits to using this code over my original - other than code brevity? – Alan Sep 05 '12 at 03:08
  • You should, when posible, always use native classes and objects, this tends to be faster and more easy to implement than crude strings. – Yefb Sep 05 '12 at 22:25