错误检查在Javascript中不起作用

Error checking is not working in Javascript

本文关键字:不起作用 Javascript 检查 错误      更新时间:2023-09-26

我目前正在学习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>

您有一些语法错误

  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>