简体   繁体   English

计算器-显示第一个计算,但第二个不显示

[英]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'>&times;</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'>&times;</a><p>Total Cost: $"+CostGrass2+"</p></div>");
  });
  //End Grass

Here is a demo: 这是一个演示:

Demo Of Code 代码演示

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.

相关问题 为什么当我单击第二张图像时却没有弹出第一张图像? - Why when I click on the second image it doesn't pop up as the first one? 第二个模式不会在第一个模式上放置背景 - Second modal doesn't put backdrop on first one 首次点击会显示验证错误,并且无效。 第二次点击就可以了 - First Click shows validation error and doesn't work. Second click it works fine 确保仅当第一个OR元素不存在时,正则表达式才会移动到第二个OR元素 - Insure that regex moves to the second OR element only if the first one doesn't exist 以两种形式发布数据,第一种形式在填写第二种形式之前不会发送任何数据 - Post data in two forms, The first form doesn't send any data until the second one is filled out 它在第一次点击时不起作用,但在第二次点击时起作用。 如何解决? - It doesn't work on the first click, but does on the second one. How to fix that? 匹配第二个数字(如果第一个不存在) - Match second number, if the first doesn't exist 首先总结,第二不-&gt;为什么? - First sums, second doesn't -> why? 谁能解释这两个代码之间的区别? 又为什么第二个有效而第一个无效呢? - Can anyone explain the difference between those two codes? And why does the second one work and the first doesn't? 切换高度时,jQuery计算不符合预期 - jQuery calculation doesn't add up as expected when toggling height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM