在值=""中打印JS变量属性 [英] Printing a JS variable in a value="" attribute
本文介绍了在值=""中打印JS变量属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用动态值实现Paypal按钮。例如,当我输入值=300.00时,按钮工作正常。
I'm trying to implement a Paypal button with a dynamic value. When I enter, for example, value="300.00", the button works fine.
但是,如果按照我的方式操作,按钮将无法正常工作。变量价格是JS变量,它包含我需要输入Paypal按钮的信息。
However, if I do it my way, the button doesn't work properly. The variable price is the JS variable that holds the information I need to put into the Paypal button.
以下是错误的代码行:
<input type="hidden" name="amount" value="<script>document.write(price)</script>.00">
推荐答案
在javascript中:
In javascript:
document.write('<input type="hidden" name="amount" value="' + price + '">')
或者更好
var div = document.createElement('div');
div.innerHTML = '<input type="hidden" name="amount" value="' + price + '">';
document.body.appendChild(div);
DEMO
这篇关于在值=""中打印JS变量属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文