【发布时间】:2017-09-30 16:19:02
【问题描述】:
编辑:用 IF 做了一个新功能......仍然没有返回 -.-。我不明白为什么。
我正在尝试在此函数中返回计算税金、总计和总计 + 税金的结果。我希望将结果打印在 h3 中,如下所示:
function taxesrepas(option){
var soustot;
var taxes;
var total;
var soustot;
if (option = getelementbyid("spaghetti"))
total += 9.62;
taxes += 0.67;
soustot +=8.95;
if (option = getelementbyid("lasagne"))
total += 10.70;
taxes += 0.75;
soustot += 9.95;
if (option = getelementbyid("salade"))
total += 6.30;
taxes += 0.45;
soustot += 5.95;
if (option =getelementbyid("escargot"))
total += 5.32;
taxes += 0.37;
soustot += 4.95;
document.getElementById("taxes").innerHTML = taxes;
document.getElementbyid("total").innerHTML = total;
document.getElementbyid("soustot").innerHTML = soustot;
}
<h3 id="choix"></h3>
<h3 id="taxes" onclick= "taxesrepas(this.value)">taxes:</h3>
<h3 id="soustotal" onclick= "taxesrepas(this.value)">Sous-total:</h3>
<h3 id="total"onclick= "taxesrepas(this.value)">Total:</h3>
这个函数应该是根据用户在选择菜单上的选择计算不同价格的函数。问题是由于某种原因它不会返回任何东西。我希望在这些 h3 中打印结果。
这是我希望它看起来像的示例。随着选择菜单的变化,税费和价格也会发生变化。
[URL=https://www.hostingpics.net/viewer.php?id=204967Capturedecran20170930a121531.png][IMG]https://img11.hostingpics.net/pics/204967Capturedecran20170930a121531.png[/IMG][/URL]
【问题讨论】:
-
没有/使用 value 属性,也不会触发 onchange 事件
-
您认为
this.value在您的代码中是什么?或者更好,你想要它是什么? -
我试图从 if 语句中返回价格,我使用 this.value 因为它是一个数字。我对此真的很陌生,我不太确定如何修复我的功能 -.-,现在我看到我缺少 {} 并更正那个 rn
-
I am trying to return the prices from the if statements为什么?无论如何,您都不使用函数的返回值。而且,即使函数是正确的,你调用它的方式也是恶意的。这就是我问this.value的原因;因为在您使用的上下文中,this上没有属性value。h3节点没有value。顺便说一句。带有switch的函数版本看起来并没有那么糟糕,但是您需要提供更多的上下文 (html 标记) 该代码应该在其中工作。
标签: javascript