【问题标题】:Update submit button text with value from input field使用输入字段中的值更新提交按钮文本
【发布时间】:2016-04-30 01:53:10
【问题描述】:

我正在尝试根据所选文本输入框的值更新表单提交按钮中的文本。

if (document.getElementById('pay_other').checked) {
   rate_value = document.getElementById('other_amount').value;
}

$('input[id="paymentAmount"]').val(rate_value);

使用上面我可以得到我需要的值。示例:100

我现在需要将提交按钮的文本更改为今天支付 100 英镑

我尝试了各种方法来插入变量 rate_value,但均未成功。

processPayment 是提交按钮的 ID

$("#processPayment").prop('value', 'Pay £'rate_value' today');

【问题讨论】:

  • 'Pay £' + rate_value + ' today' ,简单的字符串连接
  • 在 JS 中,将字符串与加号连接(连接)在一起,例如 $("#processPayment").prop('value', 'Pay £' + rate_value + ' today');
  • @PranavCBalan 谢谢!就这么简单!

标签: jquery


【解决方案1】:

在 JS 中,您将字符串与加号连接(连接)在一起,例如

$("#processPayment").prop('value', 'Pay £' + rate_value + ' today');

【讨论】:

    【解决方案2】:

    原始帖子的 cmets 已经回答了这个问题,但我想补充一点,因为您显然已经导入了 jquery,您可以通过在整个过程中使用 jquery 来简化代码:

    if ($('#pay_other').is(':checked')) {
       rate_value = $('#other_amount').val();
    }
    
    $('#paymentAmount').val(rate_value);
    
    $('#processPayment').prop('value', 'Pay £' + rate_value + ' today');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多