javascript - 如何使用文本选项更改按钮 URL
问题描述
尝试使用输入文本创建动态捐赠按钮。本质上,输入要捐赠的金额并单击按钮。输入的金额应该会更新按钮 URL,但没有任何反应。我确定我遗漏了一些明显的东西,但是这里没有可用的解决方案起作用(我尝试了 7 个)。谢谢。
document.getElementById("input-custom-donation").onchange = function() {
document.getElementById("donate-button").href = "https://securepayments.cardpointe.com/pay?details=Donation|30|"+this.value+";
}
<label for="input-custom-donation">Enter Custom Donation Amount (numbers only):</label>
<input id="input-custom-donation" type="text" name="input-custom-donation">
<br>
<br>
<a class="button" id="donate-button" href="http://test/">Donate Now</a>
解决方案
有一个错字,最后一个双引号。
document.getElementById("donate-button").href = "https://securepayments.cardpointe.com/pay?details=Donation|30|" + this.value;
提醒一下,您始终可以使用字符串插值来使字符串更具可读性,这是一个示例:
document.getElementById("donate-button").href = `https://securepayments.cardpointe.com/pay?details=Donation|30|${this.value}`