Javascript:代码不起作用但没有错误

时间:2015-11-01 21:39:17

标签: javascript

所以我希望能够在对象和目标文本栏中输入任何值来显示结果。但是,我的重置按钮工作,但过程按钮不起作用。显然这个功能没有被调用,但我不知道为什么函数没有被调用。

<html>


<head>
    <script LANGUAGE="JavaScript">
    <!-- hide script from uneducated browsers
      function newVerse(form)
      {
            obect = form.object.value
            destination = form.destination.value
            var result
            result = "Where have all the " + object + " gone?<br />"+ "Long time passing.<br />"+ "Where have all the " + object + " gone? <br />"+ "Long time ago.<br />"+ "Where have all the " + object + " gone?.<br />" + "Gone to " + destination + " everyone.<br />"+
           "When will they ever learn?<br />"+ "When will they ever learn?<br />"
            return result
      }
      // end script hiding from uneducated browsers -->
    </script>
</head>

<body>
<form>
   <b>Object</b>: <input TYPE="text" NAME="object" ID="objectID" />
   <p>
   <b>Destination</b>: <input Type="text" Name="destination" ID="destinationID" />
   </p><p>
   <textarea TYPE="textarea" NAME="text" ID="textID" rows="1" cols="50" /> </textarea>
   <p>
   <input TYPE="Reset"/>
   <input TYPE="button" VALUE="Process" onClick="newVerse(this.form)"

   </p><p>
</form>


<i>(lyrics by Pete Seeger)</i>
<hr />
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在您为object分配var时obect上有拼写错误。你也遗漏了一些var(并将你的变量变为全局变量,这是一种不好的做法)。

我建议如下:

function newVerse(form)
  {
        var object = form.object.value
        var destination = form.destination.value
        var result = "Where have all the " + object + " gone?<br />"+ "Long time passing.<br />"+ "Where have all the " + object + " gone? <br />"+ "Long time ago.<br />"+ "Where have all the " + object + " gone?.<br />" + "Gone to " + destination + " everyone.<br />"+
       "When will they ever learn?<br />"+ "When will they ever learn?<br />"
        return result
  }