【问题标题】:Uncaught ReferenceError: <function> is not defined at HTMLInputElement.onclick未捕获的 ReferenceError:<function> 未在 HTMLInputElement.onclick 中定义
【发布时间】:2017-04-04 14:03:36
【问题描述】:

我正在尝试从我的 html 页面调用一个简单的 javascript 函数。 javascript 函数将使用“lib.js”文件解密并发出警报。

未捕获的 ReferenceError:未定义解密函数 在 HTMLInputElement.onclick (test.html:18)

以下是我使用的唯一文件(以及其他依赖库文件)。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="lib.js">
function decryptfun() {
    var pass = "hjubjbjhdgyuwj";
    var encrtoken = "abcdefghijklmn";

    var p = lib.decrypt(encrtoken, atob(pass));
    }
    alert(p);
</script>
</head>

<body>
<h1>Decrypt Operation</h1>
<input type="button" onclick="decryptfun()" value="Click">
</body>
</html>

我尝试了 Stackoverflow 针对同一类型问题提供的其他建议,但没有成功。有人可以帮我找出问题的原因吗?

【问题讨论】:

  • src="lib.js" 表示您的脚本内容将被覆盖。

标签: javascript


【解决方案1】:

你的错误是因为你在里面定义了你的函数:

<script type="text/javascript" src="lib.js">

正确的方法是先关闭该脚本标记,如下所示:

<script type="text/javascript" src="lib.js"></script>

然后再次定义脚本标签来定义函数,如下所示:

<script>
function(){
    //body of function
};
</script>

<script type="text/javascript" src="lib.js"></script>
<script>
  function decryptfun() {
    var pass = "hjubjbjhdgyuwj";
    var encrtoken = "abcdefghijklmn";

    //var p = lib.decrypt(encrtoken, atob(pass)); //USE THIS IN YOUR CASE
    var p = "test"; //just for example
    alert(p);
  }
</script>
<h1>Decrypt Operation</h1>
<input type="button" onclick="decryptfun()" value="Click">

【讨论】:

    【解决方案2】:

    我的问题是我在里面定义我的函数

    <script>
        $(document).ready(function () {
            function myFunction() ....
        })
    </script>
    

    直接在script标签中定义时效果很好

    <script>
         function myFunction() ....
    </script>
    

    【讨论】:

      【解决方案3】:
      <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Person.aspx.cs" Inherits="JqueryDemo1.Person" %>
      
      <!DOCTYPE html>`enter code here`
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <script src="Scripts/jquery-3.5.1.min.js"></script>
      
          <script type="text/javascript">
      
              function SubmitData() {
                  $.ajax({
                      url: 'Person.aspx/InsertData',
                      type: 'post',
                      contentType: 'application/json;charset=utf-8',
                      dataType: 'json',
                      data: "{Name:'" + $('#txtname').val() + "', Address:'" + $('#txtaddress').val() + "', Age:'" + $('#txtage').val() + "'}",
                      success: function () {
                          alert("Data inserted successfully");
                      },
      
                      error: function () {
                          alert("insert errorr");
                      }
                  });
          </script>
      
          <title></title>
      </head>
      <body>
          <form id="form1" runat="server">
              <div>
                  <table>
                      <tr>
                          <td>Name:</td>
                          <td>
                              <input type="text" id="txtname" /></td>
                      </tr>
                      <tr>
                          <td>Address:</td>
                          <td>
                              <input type="text" id="txtaddress" /></td>
                      </tr>
                      <tr>
                          <td>Age:</td>
                          <td>
                              <input type="text" id="txtage" /></td>
                      </tr>
      
                      <tr>
                          <td>
                              <input type="button" id="btnsave" value="Submit" onclick="SubmitData()" />
                          </td>
                      </tr>
                  </table>
              </div>
          </form>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2017-05-10
        • 2022-10-30
        • 2023-01-23
        • 2016-11-03
        • 2011-01-05
        • 2016-01-02
        相关资源
        最近更新 更多