Javascript 如何在插件中使用.data()将多个数据附加到jquery对象

Javascript 如何在插件中使用.data()将多个数据附加到jquery对象,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我有两个插件 $.fn.expect = function (expectation) { return this.each(function () { $(this).data('expectation', expectation); }); } $.fn.isExpected = function () { return $(this).data('expectation').call(this); } 第一个假设附加了一系列条

我有两个插件

$.fn.expect = function (expectation) {  
    return this.each(function () {  
        $(this).data('expectation', expectation);  
});  
}  
$.fn.isExpected = function () {  
    return $(this).data('expectation').call(this);  
} 
第一个假设附加了一系列条件 示例:$input==2,$input.ToUpperCase()==input,($input/2)==1)

第二个允许我调用附加到第一个插件的数据,并在需要时计算条件

问题是,我的代码在当前状态下只允许我为每个jquery对象附加一个期望值 例如:

$(input1).expect("$(this).val() == 2");
$(input1).expect("$(this).val() == 4");
$(input1).val(2);
$(input1).isExpected(); //this returns false as it now expects 4
此外,我需要能够在代码中的不同点附加条件,这样就排除了编写类似

$(input1).expect("$(this).val() == 2 || $(this).val() == 4");

提前感谢您的建议

您可以在对象数据存储器中存储数组、对象或任何您想要的内容。因此,您可以初始化一个数组,将其存储在数据存储器中,然后在下一次调用中,只需将所需的值推送到数据存储器(或者更好,一次推送多个值):

然后您可以同时发布一个或多个期望:

$.fn.expect = function (expectation) {

    // get all the arguments as an array
    var expectations = Array.prototype.slice.apply(arguments);

    // for each matched element
    return this.each(function () {

        // gets the current expectations or set an empty array for the expectations
        var $this = $(this),
            data = $this.data('expectations') || [];

        // add all the passed expectations to the expectations array
        data = data.concat(expectations);

        // save the expectations 
        $this.data('expectations', data);

    });

};

$.fn.isExpected = function () {

    // reference to the current object
    var self = this;

    // use Array.prototype.every to test every expectation
    return $(this).data('expectations').every(function(expected) {

        // Apply the eval in a specific context using an intermediate function
        return (function() { return eval(expected); }).apply(self);

    });
}
$(input1).expect("$(this).val() == 2", "$(this).val() == 4");
检查我的工作示例:

$.fn.expect=函数(期望值){
//将所有参数作为数组获取
var期望值=Array.prototype.slice.apply(参数);
//对于每个匹配的元素
返回此。每个(函数(){
//获取当前期望值或为期望值设置空数组
变量$this=$(this),
data=$this.data('expectations')| |[];
//将所有传递的期望添加到期望数组中
data=data.concat(期望值);
//保存期望值
$this.data(“预期”,数据);
});
};
$.fn.isExpected=函数(){
//对当前对象的引用
var self=这个;
//使用Array.prototype.each测试每个期望值
返回$(this).data('expectations').every(函数(预期){
//使用中间函数在特定上下文中应用eval
return(函数(){return eval(预期);}).apply(self);
});
}
//设置输入
变量输入=$(“#测试”);
//将期望值添加到字段中,一次添加多个,或在不同实例中添加多个或单个期望值
expect(“$(this.val()>0”,“$(this.val()>10”);
input.expect(“$(this.val()==15”);
//文本按钮
$(“#按钮”)。单击(函数(){
log(input.isExpected());
});


您每次都在设置数据属性“期望值”。这是一个单一的属性。每个调用都将用给定的新值覆盖它。另外,作为旁注,在第一个方法中不需要
each()
data()
将作为jQuery逻辑的一部分对元素执行隐式的每个操作。谢谢,我可以尝试检查是否设置了预期,然后在循环中创建一个新的“预期+”索引“(如果是)。然后我必须更新isExpected以获取一个等于Expectation索引的参数这确实有帮助,谢谢,我必须测试它,看看我是否可以使用两个不同的语句对同一个对象应用两次期望值,而不是像您所展示的那样用一个语句添加两个期望值here@AndyStrackbein是的,您可以一次或在多个实例中添加单个或多个期望。我就是为了这个目的而创作的。让我知道它是否有帮助,或者我们是否可以以某种方式改进它。@AndyStrackbein修改了该片段,在不同的实例(多个和单个)中添加了多个期望值。:-)非常感谢,我以前使用了while-true循环,并执行了类似于if($(this).data('expectation'+index)!=undefined)的操作,然后将数据与数据名称上附加的索引绑定。您的更容易阅读,我将检查性能差异too@AndyStrackbein很好,如果您需要代码片段方面的任何进一步帮助,请告诉我。