Javascript 不会计算总价
这是一个作业,当我点击提交按钮时,我无法让它输出任何内容。它需要在XHTML 1.0严格按照讲师。谢谢大家!Javascript 不会计算总价,javascript,xhtml-1.0-strict,Javascript,Xhtml 1.0 Strict,这是一个作业,当我点击提交按钮时,我无法让它输出任何内容。它需要在XHTML 1.0严格按照讲师。谢谢大家! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> "Calculate Shipping" </title>
<script type="text/javascript">
// <![CDATA[
function calculateShipping() }
var price = parseFloat(document.getElementById('price').value);
This will add $1.50 to any purchases that are less than or equal
to $25.00.
if (price <= 25){
price = (price) + 1.5;
} else {
//return price * 10 / 100
var percentToAdd=(price) * .1;
price=(price)+(percentToAdd);
}
document.getElementById('result').innerHTML='Total Order Cost:
'+price;
// ]]>
</script>
</head>
<body>
<h1>Enter Purchase Price</h1>
<form action="#">
<div id="result">
<input type="text" name="price" id="price" />
<input type="button" value="Submit" onclick="calculateShipping(); return
false;" />
</div>
</form>
</body>
</html>
“计算运费”
//
输入购买价格
<代码> > P>这是在JS的中间做的:
This will add $1.50 to any purchases that are less than or equal to $25.00.
删除或注释它
还有这个
document.getElementById('result').innerHTML='Total Order Cost:
'+price;
应该在同一条线上
在函数开始时还需要正确的开始大括号,在函数结束时还需要正确的结束大括号
使用浏览器调试器
顺便说一句,你不必对括号这么开明
编辑
给你
<script type="text/javascript">
function calculateShipping() {
var price = parseFloat(document.getElementById('price').value);
document.getElementById('result').innerHTML='Total Order Cost: '+ (price <= 25 ? price + 1.5 : price * 0.1);
}
</script>
函数calculateShipping(){
var price=parseFloat(document.getElementById('price').value);
document.getElementById('result').innerHTML='Total Order Cost:'+(price既然您要求的是解释,而不仅仅是答案,下面是您的解释:
正如我在评论中所说,您的代码存在多个问题,我将在问题旁边或下面的评论中解决每个问题,指出问题所在以及原因
function calculateShipping() } // This is not an opening bracket.
// All functions(by my understanding of js) need an opening curly bracket({)
// in order to begin their declaration. You also need a closing curly bracket
// (}) to end it.
var price = parseFloat(document.getElementById('price').value);
This will add $1.50 to any purchases that are less than or equal
// On top of the fact that the 'this' keyword is reserved, the browser is
// going to treat this as multiple undefined variables, objects, etc,
// and unless this line is commented out, as this explanation is, your code
// will not work.
to $25.00.
// ^ same thing here; 'to' is undefined, as is $25, which is likely going to be
// treated as an variable holding an object. This is because the dot(.)
// is used to access either properties(variables) or methods(functions) of an
// object.
if (price <= 25){
price = (price) + 1.5;
} else {
//return price * 10 / 100
var percentToAdd=(price) * .1;
price=(price)+(percentToAdd);
}
document.getElementById('result').innerHTML='Total Order Cost:
'+price;
// Javascript does not do multi-lines like php; you either have to escape
// them or, you have to quote them. I will give examples of this below.
// And here.. here you do nothing. As noted above, you need a closing
// curly bracket to end your function declaration.
鉴于上述情况,some_var
的内容如下:
这是一个字符串这是另一个字符串
如果您希望代码位于单独的行上,那就另当别论了。您需要html来实现这一点。具体来说,就是:断开标记:
var some_var = 'this is a string<br />this is a string on another line';
Javascript不会“做”多行,除非你转义每行。除此之外,你永远不会使用正确的括号打开函数,也不会关闭函数。你的浏览器有Javascript控制台是有原因的。在几乎任何现代浏览器中都可以点击F12。我不确定我是否理解你说的话?我在HT中使用它的时候确实可以使用它ML.我拒绝相信你;你的代码语法不正确。哦,它肯定比这更简单。我刚开始根据验证工具告诉我的内容进行更改。现在它通过了验证,但不起作用。我确实通过调试器运行了它,它说calculateShipping未定义。这不是已经定义了吗在函数命令下?…但是JS肯定没有工作它仍然告诉我calculateShipping是未定义的,是混乱的代码还是我缺少了一些简单的东西?我刚刚注意到你对代码的编辑…这就是我所需要的,而不是我创建的混乱?@user3199950..第二个开口括号是从哪里来的?@代达罗斯-我的打字错误-fixed@EdHeal无意冒犯,但是你要解释你的答案吗?这个用户是初学者。仅仅给他们代码是行不通的。我现在正在写我自己的答案来解决这个问题,只是觉得有必要提醒你我的感受。
var some_var = 'this is a string ' +
'this is another string';
var some_var = 'this is a string<br />this is a string on another line';
var some_var = (1 < 2) ? 'true' : 'false';
// boolean is true is false
price = (price <= 25)?(price+1.5):(price+(price* .1));