GabiMe
GabiMe

Reputation: 18473

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..

Upvotes: 12

Views: 27114

Answers (3)

Rudi Visser
Rudi Visser

Reputation: 21979

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" id="field1" value="" />
<input type="text" name="field1_real" id="field1_real" value="" />

jQuery:

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

Upvotes: 19

Why don't you use a hidden field?

<input type="hidden">

Upvotes: 0

fearofawhackplanet
fearofawhackplanet

Reputation: 53388

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.

Upvotes: -1

Related Questions