medoix
medoix

Reputation: 1179

jQuery Field Change Submit not working

I have the below form that i want to remove the submit button from (or hide it).

<form action="cart.php" method="post">
<input name="quantity" type="text" value="3" size="1" maxlength="2" />
<input name="adjustBtn" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="1" />
</form>

In a jQuery file called test.js that i have included in the page i have the bellow that should submit the form when the input field "quantity" has changed, but it does not, i cannot get it to work and i have tried all sorts of form submit functions from other posts on SO. One person even said that if the submit button had a name of submit it would not work but i do not have that.

$(document).ready( function() {

    $('#quantity').change(function(){
       //$(this).closest("form").submit(); <-- commented out as not working either.
       //$(this).closest('form')[0].submit(); <-- commented out as not working either.
       $('#adjustBtn').submit();
    });

} );

Anyone have any idea why it wont submit the form? I have tested in chrome and IE.

Upvotes: 1

Views: 1277

Answers (2)

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91299

You need to submit the form instead of the button:

$("#adjustBtn").closest("form").submit();

Also, you didn't give an id to your adjustBtn, thus $("#adjustBtn") won't work;

<input id="adjustBtn" name="adjustBtn" type="submit" value="change" />

The same thing happens with #quantity, that's why the change handler doesn't get fired, you need to also add the id:

<input id="quantity" name="quantity" type="text" value="3" size="1" maxlength="2" />

Upvotes: 5

AdamGold
AdamGold

Reputation: 5051

It should be:

<form action="cart.php" method="post" id="autoSubmit">
<input name="quantity" type="text" value="3" size="1" maxlength="2" />
<input name="item_to_adjust" type="hidden" value="1" />
</form>

And the jQuery:

$(document).ready( function() {

    $('#quantity').change(function(){
       $('#autoSubmit').submit();
    });

} );

Upvotes: 1

Related Questions