jquery:两个数字的百分比

编程入门 行业动态 更新时间:2024-10-12 01:30:24
本文介绍了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 =ajax.googleapis/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="ajax.googleapis/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:两个数字的百分比

本文发布于:2023-11-29 11:37:31,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百分比   两个   数字   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!