jquery:两个数字的百分比 [英] jquery: percentage of two numbers

查看:414
本文介绍了jquery:两个数字的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDITED



感谢所有提供支持的人......我希望能够与您分享最佳工作脚本帮助正在寻找相同解决方案的其他人:

  $(document).ready(function(){
$ (#price1,#price2)。keyup(function(){
var priceOne = parseFloat($(#price1)。val());
var priceTwo = parseFloat($( ($(#price1)。val());
var rate = parseFloat($(#rate)。val());
if ;& $(#price2).val()){
$('#rate').val((priceTwo-priceOne)/ priceOne * 100).toFixed(2));



$ b $(#rate)。keyup(function(){
var priceOne = parseFloat($(#price1 ).val());
var rate = parseFloat($(#rate)。val());

if($(#rate)。val )& $(#price1)。val()&& $(#price2)。val()){
$('#price2')。val(((priceOne) * rate)/ 100 + priceOne).toFixed(2));
}
});
})

您也可以在 LINK






初步问题: 请帮忙计算两个数字之间的百分比。我尝试过一种方式,但我没有成功。请告诉我什么是错误的,或者我会很感激,如果你能推荐其他脚本可以帮助我



我的脚本:

 < html> 
< head>
< script type =text / javascript>
$(#rate)。text(function(){
var result =(parseInt(($(#price1)。text(),10)* 100)/ parseInt($ (#price2)。text(),10));
if(!isFinite(result))result = 0;
返回结果;
});?
< / script>

< / head>
< body>
< div id =price1>< label>< input id =price1type =text>< / label>< / div>
< div id =price2>< label>< input id =price2type =text>< / label>< / div>
< div id =rate>< label>< input id =ratetype =text>< / label>< / div>


< / body>
< / html>


解决方案

使用 val / code>代替 text()作为输入元素,使用 $(function(){})等待DOM准备就绪。

 < html> 
< head>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js>< / script>
< script type =text / javascript>
$(function(){
$(#price1,#price2)。change(function(){// change on
var result = parseFloat(parseInt($( #price1).val(),10)* 100)/ parseInt($(#price2).val(),10);
$('#rate').val(result ||' '); //在#rate
})
})中显示值;
< / script>
< / head>
< body>
< div id =price-div1>< label> price1< / label>< input id =price1type =text>< / div>
< div id =price-div2>< label> price2< / label>< input id =price2type =text>< / div>
< div id =rate-div>< label> rate< / label>< input id =ratetype =text>%< / div>
< / body>
< / html>


EDITED

Thank you for everyone who offered support... the best working script I will share with you in hope that I could help others who is looking for the same solution:

    $(document).ready(function(){
$("#price1, #price2").keyup(function() {
  var priceOne = parseFloat($("#price1").val());
  var priceTwo = parseFloat($("#price2").val());
  var rate = parseFloat($("#rate").val());
  if ($("#price1").val() && $("#price2").val()){     
  $('#rate').val(((priceTwo - priceOne) / priceOne * 100).toFixed(2));
}

});

$("#rate").keyup(function() {
  var priceOne = parseFloat($("#price1").val());
  var rate = parseFloat($("#rate").val());

   if ($("#rate").val() && $("#price1").val() && $("#price2").val()){
 $('#price2').val(((priceOne * rate)/ 100 + priceOne).toFixed(2));
}
});
})

Also you can test it following this LINK


INITIAL QUESTION:

Please help to calculate the percentage between two numbers. I tried one way, but I did not succeed. Please tell me what is wrong, or I will appreciate if you can recommend other script which could help me

my script:

<html>
<head>
 <script type="text/javascript">
$("#rate").text(function() {
    var result = (parseInt(($("#price1").text(), 10) * 100)/ parseInt($("#price2").text(), 10));
    if (!isFinite(result)) result = 0;
    return result;
});?
</script> 

</head>
<body>
<div id="price1"><label><input id="price1" type="text"></label></div>
<div id="price2"><label><input id="price2" type="text"></label></div>
<div id="rate"><label><input id="rate" type="text"></label></div>


</body>
</html>

解决方案

use val() instead of text() for input element, use $(function(){}) to wait DOM is ready. And also don't use same ID to elements.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  $("#price1, #price2").change(function() { // input on change
    var result = parseFloat(parseInt($("#price1").val(), 10) * 100)/ parseInt($("#price2").val(), 10);
    $('#rate').val(result||''); //shows value in "#rate"
  })
});
</script> 
</head>
<body>
<div id="price-div1"><label>price1</label><input id="price1" type="text"></div>
<div id="price-div2"><label>price2</label><input id="price2" type="text"></div>
<div id="rate-div"><label>rate</label><input id="rate" type="text">%</div>
</body>
</html>

这篇关于jquery:两个数字的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆