不会计算总价

时间:2014-01-16 03:09:10

标签: javascript xhtml-1.0-strict

这是一项作业,当我点击“提交”按钮时,我无法输出任何内容。根据教师的说法,它需要在XHTML 1.0 Strict中。谢谢!

<!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>

2 个答案:

答案 0 :(得分:1)

在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>

答案 1 :(得分:1)

既然你要求解释,而不只是答案,这是你的解释:

正如我在评论中所说,您的代码存在多个问题,我会在问题的相邻或下方发表评论,说明问题所在,以及原因。

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.

现在以正确的方式编写此函数;

function calculateShipping() { // opening curly brackte
    var price = parseFloat(document.getElementById('price').value);
    /* This will add $1.50 to any purchases that are less than or equal
       to $25.00.
    */
    // ^ a multi-line comment.
    if (price <= 25){
        price = (price) + 1.5;
        // This is really fine, except you don't really need () around 'price'.
    } else {
        //return price * 10 / 100
        var percentToAdd=(price) * .1;
        price=(price)+(percentToAdd);
        // Same thing as above regarding parenthesis.
    }

    document.getElementById('result').innerHTML='Total Order Cost: \
    '+price;                                    // Escape the line ^
} // Finally, end the function declaration.

如上所述,有多种方法可以计算额外的行数;这是一个:

var some_var = 'this is a string ' +
'this is another string';

鉴于上述情况,some_var的内容将为:

  

这是一个字符串,这是另一个字符串

如果您希望代码分开,那就是另一个故事。你需要html ...具体来说;中断标签:

var some_var = 'this is a string<br />this is a string on another line';

最后但并非最不重要的是,你的if / else可以简化为三元运算符;三元运算符是if / else子句的(通常)单行方法。这是一个例子:

var some_var = (1 < 2) ? 'true' : 'false';
        //     boolean   is true   is false

在上面,1小于2,因此,some_var将被设置为字符串'true'。否则,它将被设置为“false”。因此,您的上述if / else可以简化为:

price = (price <= 25)?(price+1.5):(price+(price* .1));