Javascript onclick 对象不是函数

标签 javascript jquery

所以我几个小时以来一直在尝试解决这个问题

这是我收到的错误:

error

我不明白是什么导致了错误,任何见解将不胜感激!

这是我的代码,忽略 smarty 语法:

<html>
    <head>
        <title>{$title}</title>
        {include file='header/header.tpl'}
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="container" style="padding-top: 5%;">
        <form action="" method="POST">
            <center>
                <img src="" style="height: 70px;">
            </center>
            <div class="form-signin">
                <input type="email" class="form-control" style="font-size: 10pt;" id="email" placeholder="Email Address" required autofocus>
                <input type="password" class="form-control" style="font-size: 10pt;" id="password" placeholder="Password" required>
                <input type="submit" class="btn btn-warning btn-sm" value="Login" data-loading-text"Login" id="login" onClick="login();" style="outline: none;">
            </div>
        </form>
    </div>
    {literal}
    <script type="text/javascript">
        $(function() {
            function login() {
                alert('asdf');
            }
        });
    </script>
    {/literal}
    </body>
</html>

最佳答案

将登录移到就绪函数之外:

<script type="text/javascript">
    $(function() {
        function login() {
            alert('asdf');
        }
    });
</script>

致:

<script type="text/javascript">
        function login() {
            alert('asdf');
        }
</script>

问题是登录仅存在于就绪函数范围内。示例

function say() {

   function hello() {
       alert("hello");
   }

   // call hello inside the say scope
   hello(); // this displays the alert
}


say(); // this will show the alert

hello(); // error function is not defined. because it is out of scope

关于Javascript onclick 对象不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596018/

相关文章:

javascript - 使用通配符(如 'includes')从 module.exports 对象数组中检索值

javascript - 无法在 Node js 教程中提交表单

jquery - 使用 Angular 5 中的指令激活省略号时如何激活工具提示?

javascript - 使用 javascript/jquery 增量读取 http 数据流

php - 使用 javascript 访问 php 文件中的 session 变量?

javascript - 需要一种在 MSIE8 中强制重绘 <select> 的方法

javascript - 如何在 Kendo UI Scheduler 控件中捕获空单元格单击事件

JavaScript 通过按钮删除 li 元素

javascript - IDE 与 jQuery 类似的界面(调整 div 的大小)

javascript - 绝对定位旋转 div 的 Angular