我想构建一个简单的计算应用程序。用户输入成本价格和销售价格,点击提交后我希望显示利润。我想在jQuery中执行此操作,而不仅仅是在标准的javascript中。
如何进行此计算是jQuery并以HTML格式显示答案。
HTML CODE
<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())
});
});
答案 0 :(得分:2)
You just have to update the html inside the div(with printOut class) with the answer you get after substracting the two amounts.
$(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>
答案 1 :(得分:1)
所以,我已经为你的代码做了一些改动,现在它应该正常工作。首先,我在提交按钮上将类型更改为按钮,并对jquery进行了一些更改。
不要忘记将jquery包含在你的项目中。请参阅以下代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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="button" value="Submit">
</form>
<p>The profit is:</p>
<div id="profitOut"></div>
&#13;
tests/_support/AcceptanceHelper.php
&#13;
答案 2 :(得分:0)
You have to give your paragraph an id and then $("#IdOfParagraph").append(result);
where result is the result of you calculation stored in a variable as sample
答案 3 :(得分:0)
Firs you should change
<input type="submit" value="Submit">
to
<input id="submit" type="submit" value="Submit">
You need an ID for your input. Try this, you didn't set output to div
$(document).ready(function () {
$("#submit").click(function(e){
e.preventDefault();
$('.profitOut').html(parseInt($("#salesPrice").val()) - parseInt("#costPrice").val()));
});
});