错误检查在Javascript中无效

时间:2016-05-12 08:30:13

标签: javascript

我目前正在学习Javascript,并且遇到以下脚本的问题。好像我还没有宣布这些功能。任何帮助将不胜感激。感谢。

<!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>
  <title>Chapter 7: Example 4</title>
  <script type="text/javascript">
    function btnCheckForm_onclick() {
        var myForm = document.form1;

        if (myForm.txtAge.value == "" || myForm.txtName.value == "") {
          alert("please complete the form");
          if (myForm.txtName.value == "") {
            myForm.txtName.focus();
          } else {
            myForm.txtAge.focus():
          } else {
            alert("Thanks for completing the form");
          }

        }

        function txtAge_onblur() {
          var txtAge = document.form1.txtAge;
          if (isNaN(txtAge.value) == true) {
            alert("please enter a valid age");
            txtAge.focus();
            txtAge.select();
          }
        }

        function txtName_onchange() {
          window.status = "Hi " + document.form1.txtName.value;
        }
  </script>

  <body>
    <form action="" name="form1">
      Please enter the following details:
      <p>
        Name:
        <br />
        <input type="text" name="txtName" onchange="txtName_onchange()" />
      </p>
      <p>
        Age:
        <br />
        <input type="text" name="txtAge" onblur="txtAge_onblur()" size="3" maxlength="3" />
      </p>
      <p>
        <input type="button" value="Check Details" name="btCheckForm" onclick="btnCheckForm_onclick()">
    </form>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

您有一些语法错误

  function btnCheckForm_onclick() {
        var myForm = document.form1;

        if (myForm.txtAge.value == "" || myForm.txtName.value == "") {
          alert("please complete the form");
          if (myForm.txtName.value == "") {
            myForm.txtName.focus();
          } else {
            myForm.txtAge.focus();
          } }
		  else {
            alert("Thanks for completing the form");
          }
        
    }

        function txtAge_onblur() {
          var txtAge = document.form1.txtAge;
          if (isNaN(txtAge.value) == true) {
            alert("please enter a valid age");
            txtAge.focus();
            txtAge.select();
          }
        }

        function txtName_onchange() {
          window.status = "Hi " + document.form1.txtName.value;
        }
    <form action="" name="form1">
      Please enter the following details:
      <p>
        Name:
        <br />
        <input type="text" name="txtName" onchange="txtName_onchange()" />
      </p>
      <p>
        Age:
        <br />
        <input type="text" name="txtAge" onblur="txtAge_onblur()" size="3" maxlength="3" />
      </p>
      <p>
        <input type="button" value="Check Details" name="btCheckForm" onclick="btnCheckForm_onclick()">
    </form>