所以我一直试图解决这个问题
这是我得到的错误:
我不明白导致错误的原因,任何见解都会受到赞赏!
这是我的代码,忽略了聪明的语法:
<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>
答案 0 :(得分:2)
将登录名移到就绪功能之外:
<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
答案 1 :(得分:0)
login
函数是在ready
事件处理程序中创建的,因此它只存在于该函数中。
将它放在事件处理程序之外,以便它存在于全局范围内,然后可以从click事件处理程序中调用它:
<script type="text/javascript">
function login() {
alert('asdf');
}
$(function() {
});
</script>
考虑使用表单的submit
事件而不是按钮上的click事件。然后,如果用户通过在字段中按Enter键提交表单,它也将起作用。还要考虑在脚本中而不是在标记中绑定事件:
<script type="text/javascript">
$(function() {
$('form').submit(function(e){
e.preventDefault(); // if you want to stop the submit and do something else'
alert('asdf');
});
});
</script>