未捕获的引用错误:未定义注册
Uncaught ReferenceError: reg is not defined
我试图使用 jquery、ajax 和 php 构建登录和注册表单。但是当我按下注册按钮时,我会收到此错误。
<!-- Formular for signing up -->
<form method="post">
<div class="form-group">
<label> Username </label>
<input type="text" class="form-control" name="newusername">
</div>
<div class="form-group">
<label> Password </label>
<input type="password" class="form-control" name="newpassword">
</div>
<div class="form-group">
<label> Your club </label>
<input type="text" class="form-control" name="newclub">
</div>
<button type="button" onClick='reg' class="btn btn-success">Sign up!</button>
</form>
这是我的脚本代码
$(document).ready(function () {
// Function for registrate of new users
function reg(newusername, newpassword, newclub) {
$.post('class/callingClass.php', {
newusername: 'newusername',
newpassword: 'newpassword',
newclub: 'newclub'
});
};
});
从onXYZ
属性事件处理程序调用的函数必须是全局函数(这是不使用它们的几个原因之一)。您的reg
函数不是全局函数,它很好地包含在您的ready
回调中(这是一件好事™,全局命名空间已经非常拥挤并且容易发生冲突)。
另外,onClick='reg'
不起作用,则必须onClick='reg()'
.
相反,通过on
动态挂reg
:
<button type="button" id="btn-reg" class="btn btn-success">Sign up!</button>
<!-- Removed onClick, added id -->
和
$(document).ready(function () {
$("#btn-reg").on("click", reg); // <== Added
// Function for registrate of new users
function reg(newusername, newpassword, newclub) {
$.post('class/callingClass.php', {
newusername: 'newusername',
newpassword: 'newpassword',
newclub: 'newclub'
});
};
});
我在上面使用了id
,但它不一定是id
,这只是查找元素的一种方法。任何能找到它的CSS选择器都可以。
使用 onClick='reg()'
.您需要在此处执行函数调用,这是正确的语法。
更新 :您还需要将函数移到$(document).ready(function(){})
之外。
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 未捕获的引用错误:未定义注册
- Yii:Firebug控制台(函数未定义),当使用clientscript注册函数时
- 注册MouseMove:事件对象始终未定义
- 注册表.无法获取属性'set'未定义或空引用的
- 角度 - 参数“注册控制器”不是一个函数,未定义
- 未捕获的引用错误: 未定义注册用户帐户
- Dojo:使用注册表.byID返回“未定义”