Using a textfield to change form hidden field
I'm using Jquery with Ruby on Rails (3.1) and am having some problem with updating a hidden field via javascript.
Basically I have a textfield, and I'm trying to use ajax to update two different form hidden fields (one form for google checkout, one for amazon payments).
My Javascript looks like so:
$(function() {
$('#payment_amount input').keyup(function() {
var quantity = $('#payment_amount input').val();
$("input[name='amount']").val("USD" + quantity);
$("input[name='item_price_1']").val(quantity);
return false;
});
});
I have this in my application.js file for the time being (not yet in my .js.coffee, need to look into coffee script yet).
I think this javascript is correct, as I take the value from the input field, and it should update the input fields with the names 'amount' and 'item_price_1'. I thought maybe my selectors were wrong, so here's my html:
<input id="payment_amount" type="text" size="30" name="payment[amount]"> #textfield to change form hidden fiels
<form method="POST" action="https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/.... accept-charset="utf-8">
<input type="hidden" name="item_price_1" value="20"/开发者_如何学Python> #what I want to change
....
</form>
<form action="https://authorize.payments.amazon.com/pba/paypipeline" method="post">
<input type="hidden" name="amount" value="USD 20" > #also change this
</form>
This is all comes from a new.html.erb file. I do not have a new.js.erb file, do I need one?
Right now, this javascript doesn't seem to do a thing.
Any help would be appreciated.
Change this line:
$('#payment_amount input').keyup(function() {
var quantity = $('#payment_amount input').val();
To this:
$('#payment_amount').keyup(function() {
var quantity = $(this).val();
You're using the wrong selector for your payment_amount field. You can see it working here.
精彩评论