HTML 和 JS 的问题

issues with html and js

本文关键字:问题 JS HTML      更新时间:2023-09-26

这太令人沮丧了。 我浏览了 W2school 教程,其中向您展示了一些零碎的东西,但它没有回答如何将它们放在一起。 我以为我理解了它,但当我把它付诸实践时,什么都没有。 Firebug 告诉我 inputEmp() 没有定义,但在.js文件中非常明显地定义了。 有人可以告诉我我遗漏了哪些小细节吗? 首先感谢 Html,然后是 .js 文件。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/payroll.css" />
<title>Payroll System</title>
<script type="text/javascript" src="scripts/payroll.js"></script> 
<script type="text/javascript" >
    var emps = new Array();
</script>   
</head>
<body>
  <h1>Jackson Payroll System</h1>
  <button type="button" onclick="inputEmp()">
    Click here to enter employees
  </button>
</body>
</html>

// payroll.js
function inputEmp() {
    var inName = "";
    var loopCt = 0
    var tArray = new Array();
    while (inName != "-1}
    {
        prompt inName = prompt("Please enter your name (enter -1 to finish)",
                               "Employee Name");
    if (inName == "-1")  { break; }
    if (inName==null || inName=="")
    {
        alert("Blank names are not allowed.  To exit enter '-1'.");
    } 
    else
        tArray[loopCt++] = inName;
    {
    }
    return tArray;
}   

是的,您忘记在返回语句之前关闭 while 循环的大括号 {。 并且报价在状态中没有关闭。

试试这个

    function inputEmp() {
    var inName = "";
    var loopCt = 0
    var tArray = new Array();
    while (inName != "-1"} {
        prompt inName = prompt("Please enter your name (enter -1 to finish)","Employee Name");
    if (inName == "-1")  { break; }
    if (inName==null || inName=="")
        {
        alert("Blank names are not allowed.  To exit enter '-1'.");
        } 
    else
        tArray[loopCt++] = inName;
    {
    }
    }
    return tArray;
    }   

你的代码中有很多问题:

var loopCt = 0应该看起来像这样var loopCt = 0;

while (inName != "-1}应该看起来像这样while (inName != "-1)

prompt inName = prompt...应该看起来像这样inName = prompt...

else 语句下有一个空{ }

这是一个 jsfiddle,向您展示更正后的代码。