未定义访问具有对象属性的jquery选择器
Undefined accessing jquery selector with object property
我在使用对象属性作为jquery选择器时遇到问题
define(["models/security/user", 'text!templates/security/registration.html'], function(SecurityUserModel, Template){
var SecurityRegistrationView;
SecurityRegistrationView = Backbone.View.extend({
initialize: function(){
// setting model
this.model = new SecurityUserModel();
// binding validation
Backbone.Validation.bind(this);
this.model.bind("validated:valid", this.valid);
this.model.bind("validated:invalid", this.invalid);
this.render();
},
form: {
"username": "#_user_username"
, "email": "#_user_email"
, "password": "#_user_password"
},
render: function(){
$(this.el).append(Template);
},
events: {
"submit form": "submit"
},
submit: function(e){
e.preventDefault();
this.model.set("username", $(this.form.username).val());
this.model.set("password", $(this.form.email).val());
this.model.set("email", $(this.form.password).val());
this.model.validate();
if (this.model.isValid) {
this.model.save();
}
},
valid: function(model, attrs){
console.log(attrs[0]);
$(this.form[attrs[0]]).parent("div.control-group").addClass("success");
},
invalid: function(model, attrs){
console.log(attrs[0]);
$(this.form[attrs[0]]).parent("div.control-group").addClass("error");
}
});
return SecurityRegistrationView;
});
铬控制台输出:
username registration.js:45
Uncaught TypeError: Cannot read property 'username' of undefined registration.js:46
password registration.js:45
Uncaught TypeError: Cannot read property 'password' of undefined registration.js:46
email registration.js:45
Uncaught TypeError: Cannot read property 'email' of undefined registration.js:46
username registration.js:45
Uncaught TypeError: Cannot read property 'username' of undefined registration.js:46
$(this.form.username).val(); // works
$(this.form[someVar]); // does not work
valid
和invalid
回调中的this
可能没有引用您的视图。将initialize
方法中的绑定修改为正确的范围:
this.model.bind("validated:valid", this.valid, this);
this.model.bind("validated:invalid", this.invalid, this);
尝试替换:
$(this.form.username)
带有:
$(this.form['username'])
或者,没有"这个":
$(form['username'])
- "密码"answers"电子邮件"也是如此
相关文章:
- jQuery选择器无法正常工作
- 将jquery选择器转换为数组
- JavaScriptDOM正常工作时JQuery选择器不工作
- 在我的情况下,使用带有变量失败的 jquery 选择器
- 换行符插入jquery选择器
- 使用javascript"这个“;用于jquery选择器
- jQuery选择器缓存问题
- jQuery 选择器,所有跨度都在 DIV 内
- JQuery选择器:如果同级具有.class,则选择td
- jQuery选择器不识别任何动态创建的HTML输入函数
- 循环遍历元素jquery选择器
- jQuery选择器错误:无法识别的表达式
- 自定义jquery选择器属性未在模拟器中激发
- Jquery选择器为空,我应该使用哪个作用域
- jQuery选择器就是用这个构建的
- jQuery 选择器混淆
- 将jQuery选择器与“this”相结合
- jQuery选择器在脚本中不起作用,但在控制台中工作
- jQuery 选择器中的简单 jQuery 语法和串联
- 全局 JQuery 选择器缓存以提高性能