jquery - 我如何使用 jQuery 进行此计算并以 HTML 形式显示答案?

标签 jquery html

我想构建一个简单的计算应用程序。用户输入成本价和销售价,点击提交后我希望显示利润。我想在 jQuery 中执行此操作,而不仅仅是在标准 javascript 中。

我如何使用 jQuery 进行此计算并以 HTML 形式显示答案。

HTML 代码

<form>
  Cost Price:<br>
  <input type="text" id="costPrice" name="firstname"><br>
  Sales Price:<br>
  <input type="text" id="salesPrice" name="lastname">
  <input type="submit" value="Submit">
</form>

<p>The profit is:</p>
<div class="profitOut"></div>  

jQuery - 不知道如何执行这部分

$(document).ready(function () {

  $("submit").click(function(){
    $(("#salesPrice").val() - ("#costPrice").val())
  });

});

最佳答案

您只需使用减去两个金额后得到的答案来更新 div 内的 html(带有 printOut 类)即可。

$(document).ready(function () {
      $("#form").submit(function(e){
          e.preventDefault();
          var x = $("#salesPrice").val() - $("#costPrice").val();
          $("#profitOut").html(x);
      });
});

HTML

<form id="form">
  Cost Price:<br>
  <input type="text" id="costPrice" name="costPrice"><br>
  Sales Price:<br>
  <input type="text" id="salesPrice" name="salesPrice">
  <input id="submit" type="submit" value="Submit">
</form>
<p>The profit is:</p>
<div id="profitOut"></div>

关于jquery - 我如何使用 jQuery 进行此计算并以 HTML 形式显示答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36645290/

相关文章:

javascript - 基于 Next Prev 值的 jQuery 验证

jquery - 文本框渲染中的奇怪问题

html - 使行高适应具有固定列的第一个元素

html - 将 FORM 数据安全地提交到单页应用程序?

javascript - 使用 jQuery 根据所选数据获取参数并将其放入 url

jquery - 需要在我的下拉菜单上使用 jquery 或 css3 产生一些效果

javascript - 将 load() 输出附加到 jQuery 中的同一个 div

html - 自定义滚动条溢出?

html - 在图像旁边放置文本

javascript - 在使用 ajax 加载页面之前使用旧的 javascript