简体   繁体   English

if/else 语句在 function 中不起作用

[英]if/else statement didn't work inside function

Hi I just writing a simple code to calculate the cheapest price by unit of eggs depend on the quantity coming in the box(12pcs,18pcs or 20pcs)嗨我只是写了一个简单的代码来计算最便宜的鸡蛋单位价格取决于盒子里的数量(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";
}

when i run that works like i wanted but if i enclose the if else statement into a function just show me the else statement only (just shows twenty not matter who statement is true)当我像我想要的那样运行它时,但如果我将 if else 语句包含在 function 中,只显示 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();


what can i fix to this work inside of a function i need to call the function in the future thanks and sorry English its my second language我可以在 function 内部解决这项工作我需要在未来调用 function 感谢和抱歉英语是我的第二语言

It's happening When The Elements You Try To Load By IDs are Not Loaded when the function triggers当 function 触发时未加载您尝试按 ID 加载的元素时,就会发生这种情况

You Can Use onLoad Event Attribute in Body Element to prevent This您可以在 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>

You Can Check Your JS Code is Working or Not By replacing document.getElementById();您可以通过替换 document.getElementById(); 检查您的 JS 代码是否正常工作to alert();提醒(); like this像这样

        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");
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM