在(myfirstnumber/ 100) mysecondnumber;
中我需要输入任何百分比值
myfirstnumber
<input type="number" value="F" class="1" id="myfirstnumber"/>
mysecondnumber
<input type="number" value="S" class="1" id="mysecondnumber"/>
<input type="percentage" value="P" class="1" id="mypercenttextbox ">
function ShowPercentage() {
var FirstNumID = $("[id$=" + "myfirstnumber").attr("id");
var FirstNumVal = $("#" + myfirstnumber).val();
var SecondNumID = $("[id$=" + "mysecondnumber").attr("id");
var SecondNumVal = $("#" + mysecondnumber).val();
var percentVal = (myfirstnumber - mysecondnumber) * (100 / mysecondnumber);
$("[id$=" + "mypercenttextbox ").val(percentVal);
}
我试图用jQuery做到这一点:
9.09090909090909
在上面的代码中,我能够获得百分比,但我得到它,例如。 10%
而我希望显示H(request('http://jsonplaceholder.typicode.com/users'))
.collect()
.map(Buffer.concat)
.flatMap(x => JSON.parse(x.toString('utf8')))
.each(user => console.log(user.id))
.done(data => console.log('DONE'));
。
任何帮助将不胜感激!
答案 0 :(得分:3)
调整示例代码“9.09090909090909”
向下
$("[id$=" + "mypercenttextbox ").val(parseInt(percentVal) + '%');
// output 9%
向上
$("[id$=" + "mypercenttextbox ").val(Math.ceil(parseInt(percentVal)) + '%');
// output 10%
作为十进制
$("[id$=" + "mypercenttextbox ").val(percentVal.toFixed(2) + '%')
// output 9.09%
我将您的小提琴更新为推荐(对我来说!)版本:
function ShowPercentage() {
var $inputs = $('input');
// get values
var firstNumVal = $inputs.eq(0).val();
var SecondNumVal = $inputs.eq(1).val();
// compute something
var percentVal = (firstNumVal - SecondNumVal) * (100 / SecondNumVal);
// set value
$inputs.eq(2).val(parseInt(percentVal) + '%');
}
$(document).ready(function() {
$('#computeSomething').on('click', ShowPercentage);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
myfirstnumber
<input type="number" value="F" class="1" id="myfirstnumber"> <br>
mysecondnumber
<input type="number" value="S" class="1" id="mysecondnumber"> <br>
Result
<input type="percentage" value="P" class="1" id="mypercenttextbox "><br>
<button id="computeSomething">Compute someThing!</button>
答案 1 :(得分:0)
这是一个如何实现您的目标的完整工作演示:
function ShowPercentage() {
var FirstNumVal = 0 + Number($("#myfirstnumber").val());
var SecondNumVal = 0 + Number($("#mysecondnumber").val());
$("#mypercenttextbox").val(Math.round(FirstNumVal/(SecondNumVal) * 100) / 100 + '%');
}
ShowPercentage();
$('input[type=number]').on('keyup', ShowPercentage);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="number" value="1" class="1" id="myfirstnumber"/>
<input type="number" value="100" class="1" id="mysecondnumber"/>
<input type="percentage" value="P" class="1" id="mypercenttextbox" readonly>
(另见this Fiddle)