如何获取jQuery元素自定义值?

时间:2014-06-16 10:22:00

标签: javascript jquery underscore.js

我正在尝试进行验证。我决定在其中添加错误消息和验证true或false,甚至是元素本身中元素的验证函数。

 _.each(this.bindings, function(keys, index) {
                var element = this.$('#'+index); //i am getting element
                if(keys.validate && keys.validate === true) {
                    element.validate = true; //i am setting attributes works
                    element.errorMessage = "user name must" //this too works
                }

                element.on('focus',function(){ //it works but i am not get the values what i added.
                    console.log(this.errorMessage); //consoles as undefined..how to fix?
                })

        })

如何将方法,属性添加到元素本身并在需要时调用它。我有50页的验证。所以我不想单独工作。

我自己进行验证,不建议我使用插件。

提前感谢。

3 个答案:

答案 0 :(得分:1)

问题是元素引用,循环内部element指向要添加新属性的jQuery对象,但在事件处理程序this内部引用的dom元素不是拥有您添加的新属性。

_.each(this.bindings, function (keys, index) {
    var element = this.$('#' + index); //i am getting element
    if (keys.validate && keys.validate === true) {
        //here element is a jQuery object not a dom element reference so
        element[0].validate = true; //i am setting attributes works
        element[0].errorMessage = "user name must" //this too works
    }

    element.on('focus', function () { //it works but i am not get the values what i added.
        console.log(this.errorMessage); //consoles as undefined..how to fix?
        //here this is dom element reference
    })

})

或在事件处理程序

中使用闭包变量element
_.each(this.bindings, function (keys, index) {
    var element = this.$('#' + index); //i am getting element
    if (keys.validate && keys.validate === true) {
        //here element is a jQuery object not a dom element reference
        element.validate = true; //i am setting attributes works
        element.errorMessage = "user name must" //this too works
    }

    element.on('focus', function () { //it works but i am not get the values what i added.
        console.log(element.errorMessage); //consoles as undefined..how to fix?
        //here this is dom element reference so use the closure variable element to obtain the newly added properties
    })

})

答案 1 :(得分:1)

尝试使用jQuery.data() - 方法:

_.each(this.bindings, function(keys, index) {
                var element = $('#'+index); //i am getting element
                if(keys.validate && keys.validate === true) {
                    element.data('validate', true); //i am setting attributes works
                    element.data('errorMessage', 'user name must') //this too works
                }

                element.on('focus',function(){ //it works but i am not get the values what i added.
                    console.log(this.data('errorMessage')); //consoles as undefined..how to fix?
                })

})

答案 2 :(得分:0)

我认为解决方案是使用.bind而不是.on:

_.each(this.bindings, function(keys, index) {
                var element = this.$('#'+index); //i am getting element
                if(keys.validate && keys.validate === true) {
                    element.validate = true; //i am setting attributes works
                    element.errorMessage = "user name must" //this too works
                }

                element.bind('focus',function(){ //it works but i am not get the values what i added.
                    console.log(this.errorMessage); //consoles as undefined..how to fix?
                })

        })