简体   繁体   中英

Replace hidden value in a form with javascript variable

I'm working with Google Checkout and am creating a simple Buy Now button. The button will be on many pages and have many values for the form.

What I'm looking to do is to use Javascript, to get a value on a text_field, and replace a hidden field value.

To get the value, I have this code:

$('#payment_quantity input').keyup(function() {
    var quantity = $('#payment_quantity input').val();
    return false;
});

Then, in the form from google, I have this:

<input type="hidden" name="item_quantity_1" value="1"/>

What I need to be able to do, is replace the value on this hidden input, with my quantity variable. I know this is probably really easy, but I'm amazingly confused at how to do this. I should have to select the hidden field, but I don't know how to update the value?

你去:

$('input[name="item_quantity_1"]').val(quantity);

You just have to construct a jQuery selector to get that field and then use the .val(xxx) method to set its value.

jQuery loves to overload the same method for different behaviors based on what you pass as arguments. In the case of .val() , if you pass no arguments, it retrieves the field value. If you pass an argument like .val(quantity) , it sets that value into the field like this:

$('#payment_quantity input').keyup(function() {
    var quantity = $('#payment_quantity input').val();
    $("input[name='item_quantity_1']").val(quantity);
    return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM