javascript - 引用错误文档未在 javascript 中定义

标签 javascript html

当我保存 bill.js 编辑器时,我正在使用括号编辑器显示一些 每个文档都出现错误,没有出现所需的输出。

HTML 代码为

<!DOCTYPE html>
<html lang="en">
<head>
    <title>split Bill</title>
    <link rel="stylesheet" type="text/css" href="normalize.css">
    <link rel="stylesheet" type="text/css" href="splitbill.css">
</head>

<body>
   <div id="container">
       <h1>Bill calculator</h1>
       <div id="calculator">
           <form>
               <label>
                   How Much your Bill ? <br>
            Rupees <input  type="text" id="billAmount">
               </label>
               <label>
                How was your Service sir?<br>
                <select id="serviceFeedback">
                    <option disabled selected value="0">
                        --Choose an option--
                    </option>
                    <option value="0.4">
                        40% -Very Good
                    </option>
                    <option value="0.3">
                        30% - Good
                    </option>
                    <option value="0.2">
                        20% - it was Ok
                    </option>
                    <option value="0.1">
                        10% - Bad
                    </option>
                    <option value="0.05">
                        5% - poor
                    </option>
                  </select>
               </label>
               <label>
                   How many people sharing the bill?<br>
                   <input type="text" id="totalpeople">
                   people
               </label>
               <button type="button" id="calculate">calculate!</button>
           </form>
       </div>
       <div id="totalTip">
           <sup>Rupees</sup><span id="tip">0.00 </span>
           <small id="each">each</small>
       </div>
   </div>

   <script type="text/javascript" src="bill.js"></script>

</body>
</html>

javascript 在这里我没有在每个文档函数中获得所需的输出有错误标志请告诉我我做什么我正在使用括号编辑器。 为什么我得到这个我正在使用 HTML 代码末尾

//create function
function calculateTip() {

    var billAmount = document.getElementById("billAmount").value;
    var serviceFeedback = document.getElementById("serviceFeedback").value;

    var numpeople =document.getElementById("totalpeople").value;

    //validation
    if (billAmount =="" ||serviceFeedback == 0) {

        window.alert("please enter some value Boss!");
        return;
    }

    //if input is empty
    if (numpeople =="" ||numpeople <= 1) {
        numpeople =1;
        document.getElementById("each").style.display ="none";


    } else {
        document.getElementById("each").style.display ="block";

    }

    var total = (billAmount * serviceFeedback) / numpeople;
    total = Math.round(total * 100) / 100;
    total = total.toFixed(2);

    //display the  tip
    document.getElementById("totalTip").style.display = "block";
    document.getElementById("tip").innerHTML = total;
}

// hide tip amount

document.getElementById("totalTip").style.display ="none";
document.getElementById("each").style.display ="none";

//clicking the button  calls our custom function

document.getElementById("calculate").onclick=function()
{
    calculateTip();
}

最佳答案

在函数“calculateTip”中我找到了这一行:

if (billAmount =="" || ServiceFeedback == 0) {

这里ServiceFeedback以大写字母而不是小写字母开头。

您得到的选择字段的选定选项如下:

let selectElement = document.getElementById("serviceFeedback");
let serviceFeedback = selectElement.option[selectElement.selectedIndex].value;

在html文件中我发现了另一个问题:

<input type="text" id="totalpeople ">

id末尾有空格!更好的是:

<input type="text" id="totalpeople">

在 javascript 文件中还有一个空白:

document.getElementById("each").style.display = " none";

更好的是:

document.getElementById("each").style.display = "none";

希望对您有所帮助。

对于ReferenceError:文档未定义我发现: ReferenceError: document is not defined (in plain JavaScript)

=> 我的问题是:

  1. bill.js 包含什么?
  2. calculateTip() 在哪里调用?
  3. 我的意思是:在创建文档之前是否有类似 document.getElementsBy... 的调用?

关于javascript - 引用错误文档未在 javascript 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691768/

相关文章:

javascript - VueJS : Getting values from multiple checkbox binding in textboxes

javascript - Map/Reduce 脚本中的 getInformation()、Map 和 Reduce 方法有何用途?

python - Python外汇计算器不符合

html - 缩小窗口时从垂直到水平分隔线的 css 响应

javascript - 如何禁用 Chrome 中的拖放功能?

javascript - 为无序列表设置 onclick() 属性

Javascript按值将数组转换为分组对象

javascript - 我们可以重写 Javascript 中的内置函数吗?

javascript - 如何避免切换 "embedded"HTML 元素?

html - 为什么我的 box div 位于本应位于其上方的两个元素后面?