[英]Caculator - First calculation shows up but the second one doesn't
I made a calculator for my website that I am still developing. 我为仍在开发中的网站制作了一个计算器。 The first form ends up calculating correctly bu the second form ends up to be always 0.
第一种形式最终正确计算出bu,第二种形式最终始终为0。
Here is my code: 这是我的代码:
HTML
<div class="col-md-4">
<div class="CostStone"></div>
<h2><img src="assets/images/Stone.png" width="64" height="64">Stone</h2>
<p>Original Price: $100 Per 16 Blocks</p>
<p style="color:red;">Price With HB Premium: $25 Per 16 Blocks</p>
<form>
<input id="StonePrice" type="hidden" value="1.56">
<input maxlength="4" id="Stone" type="text" class="form-control" placeholder="Quantity Of Items" onkeypress="return isNumberKey(event)"><br>
<p><button type="submit" class="btn btn-default" >Buy Items</button></p>
</form>
</div>
<div class="col-md-4">
<div class="CostGrass"></div>
<h2><img src="assets/images/Grass.png" width="64" height="64">Grass</h2>
<p>Original Price: $100 Per 48 Blocks</p>
<p style="color:red;">Price With HB Premium: $50 Per 48 Blocks</p>
<form id="Grass">
<input id="GrassPrice" type="hidden" value="1.04">
<input maxlength="4" type="text" id="Grass" class="form-control" placeholder="Quantity Of Items" onkeypress="return isNumberKey(event)"><br>
<p><button class="btn btn-default" type="submit">Buy Items</button></p>
</form>
</div>
JS/JQuery
//Stone
$("#Stone").on('keyup',function(){
// alert('pressed')
var CostStone= $("#StonePrice").val() * $(this).val();
var CostStone2 = CostStone.toFixed(2);
$(".CostStone").html("<div class='alert alert-info fade-in'><a class='close' href='#' data-dismiss='alert'>×</a><p>Total Cost: $"+CostStone2+"</p></div>");
});
//End Stone
//Grass
$("#Grass").on('keyup',function(){
// alert('pressed')
var CostGrass= $("#GrassPrice").val() * $(this).val();
var CostGrass2 = CostGrass.toFixed(2);
$(".CostGrass").html("<div class='alert alert-info fade-in'><a class='close' href='#' data-dismiss='alert'>×</a><p>Total Cost: $"+CostGrass2+"</p></div>");
});
//End Grass
Here is a demo: 这是一个演示:
I think this may be caused by the fact that your form has an Id of Grass too. 我认为这可能是由于您的表格也有一个草名。 Can you see if changing the id on the form element fixes the issue?
您能否看到更改表单元素上的ID是否解决了问题?
I hope this helps! 我希望这有帮助!
This is caused by the fact that your form has the id #Grass
assigned to it too. 这是由于您的表单也
#Grass
分配了ID #Grass
。 When you call $(this).val()
, it's not functioning correctly because you're getting the value from the first element that matches #Grass
, which is the form. 调用
$(this).val()
,它无法正常运行,因为您是从与#Grass
匹配的第一个元素(即表单)中获取值的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.