我为我的网站制作商店,但我无法理解为什么这个conde不会工作...这是代码:
<script>
function Price() {
var Quantity = document.getElementsByClassName('BUY')[0].value;
var Total = Quantity * 20;
if (!IsNan(Total)) {
document.getElementsByClassName('Total').innerHTML = Total;
document.getElementsByClassName('Quantity').innerHTML = Quantity;
} else {
document.getElementsByClassName('Total').innerHTML = "Invalid number";
document.getElementsByClassName('Quantity').innerHTML = 0
}
}
这是html:
<input style="margin-top:1%;" type="text" name="BUY" class="input" maxlength="1000" autocomplete="on" placeholder="Buy " onchange="Price()" required>Price for <span class="Quantity">0</span>Views:<span class="Total">0</span> Satoshis<br>
从这段代码我希望根据数量打印计算,但代码不起作用,我没有错误......
答案 0 :(得分:-1)
document.getElementsByClassName('BUY')
会搜索有&#39;购买&#39;的元素,但是您的输入有类&#39;输入&#39;
答案 1 :(得分:-2)
你在脚本中有worng类名,这就是它无法正常工作的原因。
更正后的代码 :( 只需复制并粘贴。。已将更改后的类更新为ID )
因为使用类名而不是获取正确的值而是将数量指定为undefined
refer this answer
function price(){
var Quantity=document.getElementById('buy').value;
var Total=Quantity*20;
if(Total != 'NaN'){
document.getElementById('Total').innerHTML=Total;
document.getElementById('Quantity').innerHTML=Quantity;
}else{
document.getElementById('Total').innerHTML="Invalid number";
document.getElementById('Quantity').innerHTML=0;
}
}
<input style="margin-top:1%;" type="text" name="BY" id="buy" maxlength="1000" autocomplete="on" placeholder="Buy " onkeyup="price()" required/>Price for
<span id="Quantity">0</span>
Views:<span id="Total">0</span>
Satoshis<br>