簡體   English   中英

javascript 倍數計算,百分比倍數,除法

[英]javascript multiple calculation with percentage multiple, divide

*在HTML下面,我把它上傳來上傳這個問題。 你應該忽略它。

我想執行多次計算。 我一直在搜索並采用我的東西,但一直失敗。 所以我想我需要更新每個代碼並需要幫助。

有 2 個值,A 和 B。和計算按鈕。

當我按下按鈕時,我希望它被計算為 A x 5% x 100 / B。

我感謝您的幫助。 謝謝。

忽略此 HTML

<form>
<div>
<h4>value 1:</h4>
<input type="text" id="value1">
<h4>value 2:</h4>
<input type="text" id="value2">
</div>
<div>
<h4>Operator:</h4>
<select id="operator" value="add">
<option value="add"> Add </option>
</select>
</div>
<br>
<button type="button" onclick="cal()"> Calculate </button>
<h2 id="result"></h2>
</form>

看看下面的片段。

請注意,我已使用您的 html 通過用戶輸入獲取相應的值。


 const cal = () => { const A = value1.value, B = value2.value; const res = A * (5/100) * (100 / B); result.innerText = `My result is ${res.toString().replace(".", ",")}`; //console.log(res); }
 <form> <div> <h4>value 1:</h4> <input type="text" id="value1" value=10> <h4>value 2:</h4> <input type="text" id="value2" value=20> </div> <div> <h4>Operator:</h4> <select id="operator" value="add"> <option value="add"> Add </option> </select> </div> <br> <button type="button" onclick="cal()"> Calculate </button> <h2 id="result"></h2> </form>

AX 5% X 100 / B ,我了解到您想要執行((AX 5%) X 100)/B 現在 ((AX 5%) X 100) 可以簡化為 (AX 5)。 即使有任何其他解釋,它也會導致相同的結果。

 function cal() { var input1 = document.querySelector("#value1").value; var input2 = document.querySelector("#value2").value; document.querySelector("#result").innerText = (input1 * 5)/input2; }
 <form> <div> <h4>value 1:</h4> <input type="text" id="value1"> <h4>value 2:</h4> <input type="text" id="value2"> </div> <div> <h4>Operator:</h4> <select id="operator" value="add"> <option value="add"> Add </option> </select> </div> <br> <button type="button" onclick="cal()"> Calculate </button> <h2 id="result"></h2> </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM