0

控制台日志一直说该功能未定义 onClick 此按钮。我真的不知道为什么。我有 2 个使用相同格式的其他弹出窗口,当我为每个弹出窗口按下每个按钮时,它们都说函数未定义。该页面没有说明有任何明显的 javascript 语法错误。

谢谢你的帮助

HTML

<div class="popup" id="popup-subscribe">
   <div id="close-subscribe" class="close-button"> </div>
   <div class="popup-title">Subscribe</div>
   <form method="post" name="subscribe" action="">
      <span class="label" style="margin-right: 5px">Name: </span><input type="text" placeholder="Name" name="name" /><br>
      <span class="label" style="margin-right: 5px">Email: </span><input type="text" placeholder="Email" name="email_subscribe" /><br>
      <div class="errors" id="subscribe-errors"></div>
      <div onclick="validateSubscribeForm()" class="button-medium">Sign me up!</div>
   </form>
</div>

Javascript

function validateSubscribeForm() {
var emailSubscribe =document.forms["subscribe"]["email_subscribe"].value;
var atpos=emailSubscribe.indexOf("@");
var dotpos=emailSubscribe.lastIndexOf(".");
var name = document.forms["subscribe"]["name"].value;
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=emailSubscribe.length)
  {
  $("#subscribe-errors").html("The email you entered is not valid");
  return false;
  } 
  else if (name == null || name == "") 
  {
  $("#subscribe-errors").html("Please enter a name");
  }else {
  document.subscribe.submit();
  }
}

具体来说,控制台日志错误状态: Uncaught ReferenceError: validateSubscribeForm is not defined

4

2 回答 2

2

您需要将 JavaScript 函数定义放在headHTML 的标记中:

<head>
    <!-- INCLUDE jQuery library first -->
    <script type="text/javascript">
        function validateSubscribeForm() {
            var emailSubscribe = document.forms["subscribe"]["email_subscribe"].value;
            var atpos = emailSubscribe.indexOf("@");
            var dotpos = emailSubscribe.lastIndexOf(".");
            var name = document.forms["subscribe"]["name"].value;
            if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailSubscribe.length) {
                $("#subscribe-errors").html("The email you entered is not valid");
                return false;
            } else if (name == null || name == "") {
                $("#subscribe-errors").html("Please enter a name");
            } else {
                document.subscribe.submit();
            }
        }
    </script>
</head>

<body>
    <div class="popup" id="popup-subscribe">
        <div id="close-subscribe" class="close-button"></div>
        <div class="popup-title">Subscribe</div>
        <form method="post" name="subscribe" action=""> <span class="label" style="margin-right: 5px">Name: </span>

            <input type="text" placeholder="Name" name="name" />
            <br /> <span class="label" style="margin-right: 5px">Email: </span>

            <input type="text" placeholder="Email" name="email_subscribe" />
            <br />
            <div class="errors" id="subscribe-errors"></div>
            <div onclick="validateSubscribeForm()" class="button-medium">Sign me up!</div>
        </form>
    </div>
</body>
于 2013-06-24T03:52:19.477 回答
1

您在这里错过的 Jquery。包括 Jquery 库。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
于 2013-06-24T03:50:51.197 回答