jQuery: How to modify form field before submit

But without the ugly effect that the new value is displayed in the the form.

if i do:

$('#submit_button').click(function(){ $('#field1').val('newval'); $('#form1').submit(); }); 

The #field1 will display (for a split second) the new value which is kind of ugly..

1

3 Answers

From the code above, it looks like you're completely ignoring the value of the visible form element, if so why not just put a hidden form field with the name you're using, and ignore what's posted through from the text field.

Instead, if that code wasn't real and you're planning on modifying the value instead, you could use the same technique, something like this:

<input type="hidden" name="field1" value="" /> <input type="text" name="field1_real" value="" /> 

jQuery:

$('#submit_button').click(function() { var newValue = $('#field1_real').val(); // Modify me $('#field1').val(newValue); $('#form1').submit(); }); 
3

Why don't you use a hidden field?

<input type="hidden"> 

Use a hidden field in the form to submit newval along with the original value?

I'm not really sure why you would try and do what you are doing.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like