0

这太令人沮丧了。我浏览了 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;
}   
4

2 回答 2

0

是的,您忘记在 return 语句之前关闭 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;
    }   
于 2013-09-16T23:20:26.940 回答
0

你的代码有很多问题:

var loopCt = 0应该是这样的var loopCt = 0;

while (inName != "-1}应该是这样的while (inName != "-1)

prompt inName = prompt...应该是这样的inName = prompt...

{ }else语句下有一个空

这是一个jsfiddle,它向您显示更正的代码。

于 2013-09-17T00:17:32.383 回答