javascript - 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();
我可以在函数内部解决什么问题我需要在将来调用该函数谢谢和抱歉英语是我的第二语言
解决方案
当函数触发时未加载您尝试按 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");
}
}
推荐阅读
- laravel - 如何在不使用策略所属模型的索引上使用策略?
- c - 如何在c中对数组进行切片
- angular - 角度 - 材料
在测试中不可见 - java - Redisson getLock() 对分布式锁的使用安全吗?
- r - ANOVA 因子警告和无 p 值
- javascript - 在摩纳哥获取当前的分词器
- amazon-web-services - 如何将 JSON 传递给 AWS StepFunction 中的 ECS 任务?
- c# - c# OpenXML WordprocessingDocumentType 在 TableCell 中插入 HTML 片段
- mysql - 将旋转后的 CSV 文件加载到 MySQL 数据库
- r - 在 RMD 报告中设置仪表(来自 flexdashboard)图的比例