admin管理员组

文章数量:1289620

I have some javascript that submits to an HTML form in my app upon a "click" event like so....

google.maps.event.addListener(marker, 'click', function() {
  $('form').submit()
});

However I want to change the data before I send it. Is this possible? I don't want to use Ajax $.post if I can help it. I just want to change what the form sends to my Rails controller before submit.

Thanks!

I have some javascript that submits to an HTML form in my app upon a "click" event like so....

google.maps.event.addListener(marker, 'click', function() {
  $('form').submit()
});

However I want to change the data before I send it. Is this possible? I don't want to use Ajax $.post if I can help it. I just want to change what the form sends to my Rails controller before submit.

Thanks!

Share Improve this question asked Nov 26, 2011 at 18:56 thoughtpunchthoughtpunch 1,9375 gold badges25 silver badges41 bronze badges 1
  • Sure, just update the field values before calling ".submit()". – Pointy Commented Nov 26, 2011 at 18:58
Add a ment  | 

2 Answers 2

Reset to default 6

If you already have a field with name="search" you can change it's value like so:

google.maps.event.addListener(marker, 'click', function() {

   $('form input[name="search"]').val( place.name );
  $('form').submit()
});

Or if not:

google.maps.event.addListener(marker, 'click', function() {

  $('form').append('<input type="hidden" name="search" value="'+place.name+'" />').submit()
});

Assuming place.name is known like in your previous question.

You can update the value of specific fields by id before you call submit:

$("form #fieldID").val("Value you want to change");

本文标签: javascriptJquerysubmit()Changing Form Values before submitStack Overflow