0

var result;
var gasoline = document.getElementById("gasolina").value
var alcohol = document.getElementById("alcool").value
var converte = parseInt(gasolina)

function calculo(){
    valor = document.getElementById("text")

    var calculando = function calculando(){
        var divisao = newAlcool % newGasolina
       if(divisao > 0.75){
            result = "Gasoline is better"
            valor.innerHTML = `${result}`
       }else{
           result = "alcohol is better"
           valor.innerHTML = `${result}`
       }
    }

    if(!alcohol || !gasoline || isNaN(alcohol) || isNaN(gasoline)){
        result = "Please fill in the required field"
        valor.innerHTML = `${result}`
    }else{
        newGasolina = parseInt(gasoline)
        newAlcool = parseInt(alcohol)
        calculando()
    }

}
*{
    background-image: url('img/Background.png');
    background-size: 100%;
    font-family: 'Prompt', sans-serif;
    color: black;
}

.txtmenor{
    font-size: 15px;
}

div{
    text-align: center;
    border:none;
    background: transparent;
    outline: none;
}
.logo{
    width: 150px;
    background: transparent;
}

h1{
    background: transparent;
    align-items: center;
    text-align: center;
    font-size: 18px;
    margin-bottom: 0px;
}

.calcular{
    margin-top: 15px;
    align-items: center;
    text-align: center;
    background: transparent;
    padding: 0;
    border: none;
    background: none;
}

.calcularimg{
    width: 150px;
    text-align: center;
    background: transparent;
    border-radius: 10px;
}

input{   
    margin: 0;
    color: #314591;
    background:  white;
    text-align: center;
    background-image: none;
    margin-right: 10px;
}

a{
    text-align: center;
    border:none;
    background: transparent;
    outline: none;
}
p{
    background: transparent;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Alcool ou Gasolina</title>
    <link rel="stylesheet" href="style.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Prompt:wght@300&display=swap" rel="stylesheet">
    <script src="main.js"></script>
</head>
<body>
    <header id="header">
    <!--<div><img src="img/Logo App.png" alt="Alcool Ou Gasolina" class="logo"></div>-->
    <h1>Vamos Comparar qual e o melhor para o seu carro entre alcool e gasolina </h1>
    <h1 class="txtmenor"><div>Alcool&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gasolina</div> </h1>
    
    <div><input type="number" name="" id="gasolina" placeholder="Ex:0.00" min="0">

    <input type="number" name="" id="alcool" placeholder="Ex:0.00" min="0"><br></div>
    <a hidden href="http://127.0.0.1:5500/calcular.html"> <p> Entenda o calculo </p></a> 
    <div><button onclick="calculo()" class="calcular" id="calcular">calculo</button></div>
    <h1 id="text"></h1>
</header>
</body>
</html>

我尝试了很多方法来处理这段代码,但没有用充满。我的功能可能不起作用我不确定

我的代码:

var result;
var gasoline = document.getElementById("gasolina").value
var alcohol = document.getElementById("alcool").value
var converte = parseInt(gasolina)

function calculo(){
    valor = document.getElementById("text")

    var calculando = function calculando(){
        var divisao = newAlcool % newGasolina
       if(divisao > 0.75){
            result = "Gasoline is better"
            valor.innerHTML = `${result}`
       }else{
           result = "alcohol is better"
           valor.innerHTML = `${result}`
       }
    }

    if(!alcohol || !gasoline || isNaN(alcohol) || isNaN(gasoline)){
        result = "Please fill in the required field"
        valor.innerHTML = `${result}`
    }else{
        newGasolina = parseInt(gasoline)
        newAlcool = parseInt(alcohol)
        calculando()
    }

}
4

1 回答 1

2

如果检查 isNaN 你想要,然后更新你的 if 条件

if(!alcohol || !gasoline || isNaN(alcohol) || isNaN(gasoline)) { 

}

并更新您的代码,因为您没有获得价值,您只能获得 HTML 元素。

var gasoline = document.getElementById("gasolina").value;
var alcohol = document.getElementById("alcool").value;

因为isNaN("")永远都是false

于 2021-09-05T20:32:35.737 回答