首页 > 解决方案 > if/else 语句在函数内部不起作用

问题描述

嗨我只是写了一个简单的代码来计算最便宜的鸡蛋单位价格取决于盒子里的数量(12个,18个或20个)

// var quantity of eggs//
var docena="12";
var dieciocho="18";
var veinte="20";
//type price by quantity//
var precio_docena=prompt("dozen");
var precio_dieciocho=prompt("eighteen");
var precio_veinte=prompt("twenty");
//calculate price by unit//
var precio_unidad12=precio_docena/docena;
var precio_unidad18=precio_dieciocho/dieciocho;
var precio_unidad20=precio_veinte/veinte;

//shows cheaper//    

if
(precio_unidad12<precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("minimun").innerHTML="dozen";
}
else if
(precio_unidad12>precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("med").innerHTML="eighteen";
}
else 
{
    document.getElementById("highest").innerHTML="twenty";
}

当我像我想要的那样运行它时,如果我将 if else 语句包含在一个函数中,只显示 else 语句(只显示 20,无论谁的语句是真的)

//var quantities of eggs//
var docena="12";
var dieciocho="18";
var veinte="20";
//type price by quantity//
var precio_docena=prompt("dozen");
var precio_dieciocho=prompt("eighteen");
var precio_veinte=prompt("twenty");
//calculate price by unit//
var precio_unidad12=precio_docena/docena;
var precio_unidad18=precio_dieciocho/dieciocho;
var precio_unidad20=precio_veinte/veinte;

 //shows cheaper//   
function calculate(){
if
(precio_unidad12<precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("minimun").innerHTML="dozen";
}
else if
(precio_unidad12>precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("med").innerHTML="eighteen";
}
else 
{
    document.getElementById("highest").innerHTML="twenty";
}
}
calculate();


我可以在函数内部解决什么问题我需要在将来调用该函数谢谢和抱歉英语是我的第二语言

标签: javascriptfunctionif-statement

解决方案


当函数触发时未加载您尝试按 ID 加载的元素时,就会发生这种情况

您可以在 Body 元素中使用 onLoad 事件属性来防止这种情况

<html>
    <head>
        <title>
        </title>
    </head>



<body onload="calculate();">

    <div id="minimun">a</div>
    <div id="med">b</div>
    <div id="highest">c</div>
    
</body>
    <script>
        //var quantities of eggs//
        var docena="12";
        var dieciocho="18";
        var veinte="20";
        //type price by quantity//
        var precio_docena=prompt("dozen");
        var precio_dieciocho=prompt("eighteen");
        var precio_veinte=prompt("twenty");
        //calculate price by unit//
        var precio_unidad12=precio_docena/docena;
        var precio_unidad18=precio_dieciocho/dieciocho;
        var precio_unidad20=precio_veinte/veinte;

         //shows cheaper//   
        function calculate(){
            if
            (precio_unidad12<precio_unidad18 && precio_unidad12<precio_unidad20){
                document.getElementById("minimun").innerHTML="dozen";
            }
            else if
            (precio_unidad12>precio_unidad18 && precio_unidad12<precio_unidad20){
                document.getElementById("med").innerHTML="eighteen";
            }
            else 
            {
                document.getElementById("highest").innerHTML="twenty";
            }
        }

    </script>

</html>

您可以通过替换 document.getElementById(); 检查您的 JS 代码是否正常工作 提醒();像这样

        function calculate(){
            if
            (precio_unidad12<precio_unidad18 && precio_unidad12<precio_unidad20){
                alert("dozen");
            }
            else if
            (precio_unidad12>precio_unidad18 && precio_unidad12<precio_unidad20){
                alert("eighteen");
            }
            else 
            {
                alert("twenty");
            }

        }

推荐阅读