我已經組合了一個計算器根據用戶輸入計算價格。它可以很好地處理一個輸入,但現在我必須使用第二個用戶輸入來擴展它。但是這裏有一個問題:用戶可能不想把任何東西放到現場,所以它將是空的。這就是剎車我的代碼。我可以複製計算器函數,並返回值並將這兩個值一起添加到第三個函數中,但當存在空值時它將不起作用。添加兩個javascript函數(取決於輸入)
只是爲了它的緣故,一些瑣碎的HTML代碼:
//When I only calculate with this user input, its easy
<input type="text" id="rocktext"><br>
// But how to consider this and do the same exact calculations like with the
//first one and add those two result together?
<input type="text" id="rocktext2"><br>
到底的代碼應該是這樣的:
以第一用戶輸入,計算價格(如在代碼如下)
如果(!!)有第二個用戶輸入,計算出價格並將它加到 第一個
我是一個白癡嘗試與JS或只是一個白癡在第一個?
希望聽到你,夥計們!
J.
初始JS代碼如下:
function priceCalc() {
var inputs = document.getElementById("rocktext").value;
var length = inputs.length;
var accept = 6;
var initPrice = 8;
if (inputs<=accept){
// Since the code is much simpler right now i just put the result in HTML as follows:
document.getElementById("rockpricetotal").innerHTML = initPrice + " dollars";
//I can also return the the value calculated here like so:
//retVal = initPrice;
}
else {
var intLength = parseInt(length, 10);
var lengthGap = intLength - accept;
var totals = lengthGap * 0.8 + initPrice;
var prec = totals.toPrecision(3);
// Since the code is much simpler right now i just put the result in HTML as follows:
document.getElementById("rockpricetotal").innerHTML = prec + " dollars";
// Here also the return clause can be possible with the calculation result like so:
//retVal = prec;
}
// And the final return as an alternative to the innerHTML :
// return retVal;
}
如果你使用''兩個'輸入'並給他們一個初始值爲0.00('value ='0'')? –