我正在编写一个程序,询问购买金额,然后计算折扣价。该功能有效,但属性 innerHTML 仅在我将其设置在段落 p> 中时才有效。我希望它在输入中打印出来>。所以我的问题是我需要使用什么属性。
错误消息:未捕获的 TypeError:无法将属性“innerHTML”设置为 null
<body>
<p>Enter amount of purchases:<input type =" text" id ="purchase_amount" onkeyup ="calculate_discount()"/></p>
<p>Discount price:<input type="text" id =" discount_amount"/></p>
</body>
function calculate_discount() {
var purchase_amount = document.getElementById("purchase_amount").value;
var answer;
if (purchase_amount >= 10)
{
answer = purchase_amount - (purchase_amount * .10);
}
else if (purchase_amount < 10)
{
answer = purchase_amount;
}
document.getElementById("discount_amount").innerHTML = answer;
}
谢谢