Javascript 乘以 .value

我试图从两个下拉菜单和一个文本输入中乘以 3 个数字。我知道这听起来很简单,但我已经尝试了很多小时了!


当我尝试在 Chrome 中询问开发人员工具时,它会正确返回值(在 console.log 中我写的是:kroner.value)。但我不能在“var total”行中乘以它。我必须改变某些东西的顺序吗?或者甚至重新考虑整个代码?:-)


非常感谢!


//Herfra starter udregningen af drikkepenge

function udregn() {

    var kroner = document.getElementById("kroner").value;

    var land = document.getElementById("land").value;

    var kvalitet = document.getElementById("kvalitet").value;


    //Hvis alle oplysninger ikke tastes

    if (kroner === "" || land === 0 || kvalitet === 0) {

        alert("Udfyld venligst alle felter.");

        return;

    }


    //Selve udregningen

    var total = kroner * land * kvalitet;


     //round to two decimal places

  total = Math.round(total * 100) / 100;

  //next line allows us to always have two digits after decimal point

  total = total.toFixed(2);


    //Vis drikkepengene

    document.getElementById("tipbelob").style.display = "block";

    document.getElementById("tip").innerHTML = total;

}



document.getElementById("regnformig").onclick = function () {

    udregn();

};

body {

  background-color: #a2afba;

}


#drikkepengeboks {

  width: 400px;

  height: 600px;

  background-color: #e8eaeb;

  text-align: center;

  padding: 25px;

  margin: auto;

  margin-top: 70px;

  border-radius: 30px;

  box-shadow: black 0 20px 20px 0;

}


h1 {

  text-decoration: underline;

}


p {

  margin: auto;

  width: 350px;

  text-align: center;

  margin-bottom: 65px;

}


select {

  border: gold 1px solid;

  width: 50%;

}


input {

  border: gold 1px solid;

  width: 50%;

}


h3 {

  display: flex;

  margin: auto;

  align-items: center;

  justify-content: center;

  font-size: 20px;

    margin-top: 40px;

  width: 300px;

  height: 45px;

  border-radius: 25px;

  background-color: #a2afba;

  border: gold 2px solid;

  box-shadow: black 0 4px 4px 0;

}


#tipbelob {

  margin-top: 50px;

  font-size: 30px;

  font-weight: bolder;

}


ibeautiful
浏览 412回答 2
2回答

慕妹3146593

用 HTML 中的一个点替换逗号,它就可以工作了。//Herfra starter udregningen af drikkepengefunction udregn() {&nbsp; &nbsp; var kroner = document.getElementById("kroner").value;&nbsp; &nbsp; var land = document.getElementById("land").value;&nbsp; &nbsp; var kvalitet = document.getElementById("kvalitet").value;&nbsp; &nbsp; //Hvis alle oplysninger ikke tastes&nbsp; &nbsp; if (kroner === "" || land === 0 || kvalitet === 0) {&nbsp; &nbsp; &nbsp; &nbsp; alert("Udfyld venligst alle felter.");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; //Selve udregningen&nbsp; &nbsp; var total = kroner * land * kvalitet;&nbsp; &nbsp; &nbsp;//round to two decimal places&nbsp; total = Math.round(total * 100) / 100;&nbsp; //next line allows us to always have two digits after decimal point&nbsp; total = total.toFixed(2);&nbsp; &nbsp; //Vis drikkepengene&nbsp; &nbsp; document.getElementById("tipbelob").style.display = "block";&nbsp; &nbsp; document.getElementById("tip").innerHTML = total;}document.getElementById("regnformig").onclick = function () {&nbsp; &nbsp; udregn();};body {&nbsp; background-color: #a2afba;}#drikkepengeboks {&nbsp; width: 400px;&nbsp; height: 600px;&nbsp; background-color: #e8eaeb;&nbsp; text-align: center;&nbsp; padding: 25px;&nbsp; margin: auto;&nbsp; margin-top: 70px;&nbsp; border-radius: 30px;&nbsp; box-shadow: black 0 20px 20px 0;}h1 {&nbsp; text-decoration: underline;}p {&nbsp; margin: auto;&nbsp; width: 350px;&nbsp; text-align: center;&nbsp; margin-bottom: 65px;}select {&nbsp; border: gold 1px solid;&nbsp; width: 50%;}input {&nbsp; border: gold 1px solid;&nbsp; width: 50%;}h3 {&nbsp; display: flex;&nbsp; margin: auto;&nbsp; align-items: center;&nbsp; justify-content: center;&nbsp; font-size: 20px;&nbsp; &nbsp; margin-top: 40px;&nbsp; width: 300px;&nbsp; height: 45px;&nbsp; border-radius: 25px;&nbsp; background-color: #a2afba;&nbsp; border: gold 2px solid;&nbsp; box-shadow: black 0 4px 4px 0;}#tipbelob {&nbsp; margin-top: 50px;&nbsp; font-size: 30px;&nbsp; font-weight: bolder;}<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1">&nbsp; &nbsp; <title>Drikkepengeudregner</title></head><body><div id="drikkepengeboks"><h1>Drikkepengeudregner</h1><p>Få hjælp til at udregne, hvor mange drikkepenge du skal give.&nbsp; Udfyld nedenstående og se beløbet.&nbsp; <h2>Hvad lyder regningen på?</h2>&nbsp; <input id="kroner" type="number" placeholder="Beløbet i kr."><h2>Hvor befinder du dig?</h2><select id="land">&nbsp; <option disabled selected value="0">-- Vælg venligst et land --</option>&nbsp; <option value="0.15">Danmark</option>&nbsp; <option value="0.05">Gran Canaria</option>&nbsp; <option value="0.10">Kuching</option></select><h2>Hvor god var mad og service?</h2><select id="kvalitet">&nbsp; &nbsp; <option disabled selected value="0">-- Vælg venligst din vurdering --</option>&nbsp; <option value="1.1">Rigtig god</option>&nbsp; <option value="1">Godkendt</option>&nbsp; <option value="0.9">Under forventning</option></select><h3 id="regnformig">Vis mig beløbet!</h3><div id="tipbelob">&nbsp; &nbsp; kr. <span id="tip">0.00</span>&nbsp; </div></div></body></html>

波斯汪

总数是NaN因为乘法是对无效数字执行的。这些值应该有一个小数点分隔符,而不是逗号。固定片段://Herfra starter udregningen af drikkepengefunction udregn() {&nbsp; &nbsp; var kroner = document.getElementById("kroner").value;&nbsp; &nbsp; var land = document.getElementById("land").value;&nbsp; &nbsp; var kvalitet = document.getElementById("kvalitet").value;&nbsp; &nbsp; //Hvis alle oplysninger ikke tastes&nbsp; &nbsp; if (kroner === "" || land === 0 || kvalitet === 0) {&nbsp; &nbsp; &nbsp; &nbsp; alert("Udfyld venligst alle felter.");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; //Selve udregningen&nbsp; &nbsp; var total = kroner * land * kvalitet;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // check value of total in the console&nbsp;&nbsp; &nbsp; // console.log('total', total)&nbsp; &nbsp; &nbsp;//round to two decimal places&nbsp; total = Math.round(total * 100) / 100;&nbsp; //next line allows us to always have two digits after decimal point&nbsp; total = total.toFixed(2);&nbsp; &nbsp; //Vis drikkepengene&nbsp; &nbsp; document.getElementById("tipbelob").style.display = "block";&nbsp; &nbsp; document.getElementById("tip").innerHTML = total;}document.getElementById("regnformig").onclick = function () {&nbsp; &nbsp; udregn();};body {&nbsp; background-color: #a2afba;}#drikkepengeboks {&nbsp; width: 400px;&nbsp; height: 600px;&nbsp; background-color: #e8eaeb;&nbsp; text-align: center;&nbsp; padding: 25px;&nbsp; margin: auto;&nbsp; margin-top: 70px;&nbsp; border-radius: 30px;&nbsp; box-shadow: black 0 20px 20px 0;}h1 {&nbsp; text-decoration: underline;}p {&nbsp; margin: auto;&nbsp; width: 350px;&nbsp; text-align: center;&nbsp; margin-bottom: 65px;}select {&nbsp; border: gold 1px solid;&nbsp; width: 50%;}input {&nbsp; border: gold 1px solid;&nbsp; width: 50%;}h3 {&nbsp; display: flex;&nbsp; margin: auto;&nbsp; align-items: center;&nbsp; justify-content: center;&nbsp; font-size: 20px;&nbsp; &nbsp; margin-top: 40px;&nbsp; width: 300px;&nbsp; height: 45px;&nbsp; border-radius: 25px;&nbsp; background-color: #a2afba;&nbsp; border: gold 2px solid;&nbsp; box-shadow: black 0 4px 4px 0;}#tipbelob {&nbsp; margin-top: 50px;&nbsp; font-size: 30px;&nbsp; font-weight: bolder;}<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1">&nbsp; &nbsp; <title>Drikkepengeudregner</title></head><body><div id="drikkepengeboks"><h1>Drikkepengeudregner</h1><p>Få hjælp til at udregne, hvor mange drikkepenge du skal give.&nbsp; Udfyld nedenstående og se beløbet.&nbsp; <h2>Hvad lyder regningen på?</h2>&nbsp; <input id="kroner" type="number" placeholder="Beløbet i kr."><h2>Hvor befinder du dig?</h2><select id="land">&nbsp; <option disabled selected value="0">-- Vælg venligst et land --</option>&nbsp; <option value="0.15">Danmark</option>&nbsp; <option value="0.05">Gran Canaria</option>&nbsp; <option value="0.10">Kuching</option></select><h2>Hvor god var mad og service?</h2><select id="kvalitet">&nbsp; &nbsp; <option disabled selected value="0">-- Vælg venligst din vurdering --</option>&nbsp; <option value="1.1">Rigtig god</option>&nbsp; <option value="1">Godkendt</option>&nbsp; <option value="0.9">Under forventning</option></select><h3 id="regnformig">Vis mig beløbet!</h3><div id="tipbelob">&nbsp; &nbsp; kr. <span id="tip">0.00</span>&nbsp; </div></div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript