使用输入字段中的值更新提交按钮文本

时间:2016-04-29 15:33:42

标签: jquery

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

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');

2 个答案:

答案 0 :(得分:0)

在JS中,您使用加号连接(连接)字符串,如

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

答案 1 :(得分:0)

这个问题已在原帖的评论中得到解答,但我想补充一点,因为你显然已经导入了jquery,你可以在整个过程中使用jquery来简化你的代码:

if ($('#pay_other').is(':checked')) {
   rate_value = $('#other_amount').val();
}

$('#paymentAmount').val(rate_value);

$('#processPayment').prop('value', 'Pay £' + rate_value + ' today');
相关问题