简体   繁体   English

用javascript变量替换表单中的隐藏值

[英]Replace hidden value in a form with javascript variable

I'm working with Google Checkout and am creating a simple Buy Now button. 我正在使用Google Checkout,并且正在创建一个简单的立即购买按钮。 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. 我想做的是使用Javascript,在text_field上获取一个值,并替换一个隐藏的字段值。

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选择器来获取该字段,然后使用.val(xxx)方法设置其值。

jQuery loves to overload the same method for different behaviors based on what you pass as arguments. jQuery喜欢根据您作为参数传递的内容针对不同的行为重载相同的方法。 In the case of .val() , if you pass no arguments, it retrieves the field value. 对于.val() ,如果不传递任何参数,它将检索字段值。 If you pass an argument like .val(quantity) , it sets that value into the field like this: 如果您传递.val(quantity)类的参数,它将像.val(quantity)这样将值设置为字段:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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