2 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
你正在混合jQuery在一起,JS理想情況下堅(jiān)持使用一個(gè)以避免混淆。您不需要單獨(dú)的函數(shù)添加第三個(gè)輸入值乘以的second值。
您可以在API調(diào)用函數(shù)中執(zhí)行所有這些操作。除了獲得你還需要在第三個(gè)輸入上decimals使用。toFixed()finalamount
此外,為了獲得更好的用戶體驗(yàn),我建議使用更好的.on功能,因?yàn)槟阌蓄愋吞?hào)。您可以通過(guò)單擊增加輸入來(lái)使用您的數(shù)字,新值和總數(shù)將立即反映出來(lái),而不是再次單擊或輸入。inputkey-upinputincrement
現(xiàn)場(chǎng)工作演示:
$("#validationTooltip02").on('input', function() { //input[name='calc']
let convFrom;
if ($(this).prop("name") == "btc") {
convFrom = "btc";
convTo = "usd";
} else {
convFrom = "usd";
convTo = "btc";
}
$.getJSON("https://api.coindesk.com/v1/bpi/currentprice/usd.json",
function(data) {
var origAmount = parseFloat($("input[name='" + convFrom + "']").val());
var exchangeRate = parseInt(data.bpi.USD.rate_float);
let amount;
if (convFrom == "btc")
amount = parseFloat(origAmount * exchangeRate);
else
amount = parseFloat(origAmount / exchangeRate);
$("input[name='" + convTo + "']").val(amount.toFixed(2));
//Add here
var a = parseFloat($('#a').val())
var b = parseFloat($('#b').val())
var final = a * b//final amount multiplied by 465
$('#c').val(final.toFixed(2))
});
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stacksnippets.net/scripts/snippet-javascript-console.min.js?v=1"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<form>
<input type="number" name="btc" class="form-control" id="validationTooltip02" placeholder="BTC">
<input type="number" name="usd" class="form-control" id="a" placeholder="USD" readonly>
<input type="hidden" id="b" value="465">
<input name="amount" class="form-control" type="text" placeholder="0.00000" id="c" aria-label="0.00000" readonly>
</form>

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
所以,我認(rèn)為你應(yīng)該在你已經(jīng)調(diào)用.form-controlkeyup 的函數(shù)中使用你的 add 函數(shù),如下所示:
$(".form-control").keyup(function() { //input[name='calc']
let convFrom;
if ($(this).prop("name") == "btc") {
convFrom = "btc";
convTo = "usd";
} else {
convFrom = "usd";
convTo = "btc";
}
$.getJSON("https://api.coindesk.com/v1/bpi/currentprice/usd.json",
function(data) {
var origAmount = parseFloat($("input[name='" + convFrom + "']").val());
var exchangeRate = parseInt(data.bpi.USD.rate_float);
let amount;
if (convFrom == "btc")
amount = parseFloat(origAmount * exchangeRate);
else
amount = parseFloat(origAmount / exchangeRate);
$("input[name='" + convTo + "']").val(amount.toFixed(2));
// Here goes the content of the add function
var x = parseInt(document.getElementById("a").value);
var y = parseInt(document.getElementById("b").value)
document.getElementById("c").value = x * y;
});
});
添加回答
舉報(bào)