[英]javascript object to jquery selector
I'm doing a code for forms, as have a lot of forms need to simplify the way to mask fields in the forms.For I made a class that encapsulates the ids of the fields and assign specific interactions for each. 我正在为表单编写代码,因为很多表单都需要简化掩盖表单中字段的方式,因为我做了一个封装字段ID并为每个字段分配特定交互的类。
I have code: 我有代码:
var Mask=(function() {
var me=this;
var eventInterator;
var func;
var fields;
function Mask(fd,ev,fn){
this.setFunction(fd);
this.setFunction(fn);
this.setEvent(ev);
this.execute();
}
Mask.prototype.setFields = function(fd){
fields=fd;
}
Mask.prototype.setFunction= function(fn){
func=fn;
}
Mask.prototype.setEvent= function (ev){
eventInterator=ev;
}
Mask.prototype.execute = function(){
for (var i=0;i<fields.length;i++){
loadEvent(fields[i]);
}
}
function loadEvent(field){
$(me+' '+field).on(eventInterator,function() {
func();
});
}
return Mask;
})();
when I run the follow code: 当我运行以下代码时:
function doSomeThing(){
alert("hi");
}
var fields = ['#field1','#field2','#field3','#field4'];
var mask = new Mask(fields,"keyup",doSomeThing);
I receive the error: Syntax error, unrecognized expression: [object Window] 我收到错误:语法错误,无法识别的表达式:[对象窗口]
how can I set the object of javascript class for the jquery selector? 如何为jquery选择器设置javascript类的对象?
There are 2 problems 有两个问题
First this.setFunction(fd);
首先this.setFunction(fd);
should be this.setFields(fd);
应该是this.setFields(fd);
Second 第二
In your Mask
function, me
refers to the window
object, since it is an object me + ' ' + field
will return [object Window] #field1
, that is why you are getting th error. 在您的Mask
函数中, me
指的是window
对象,因为它是一个对象,所以me + ' ' + field
将返回[object Window] #field1
,这就是您遇到错误的原因。
So change $(me+' '+field)
to $(field)
因此将$(me+' '+field)
更改$(field)
Also as @plalx said, your use of local variables is useless as every instance of Mask
will override previous values 就像@plalx所说的那样,您对局部变量的使用是无用的,因为每个Mask
实例都会覆盖以前的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.