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

Javascript 如何获取jQuery元素自定义值?,javascript,jquery,underscore.js,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) {

我正在做一个验证。在其中,我决定添加错误消息和验证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页的验证。所以我不想单独工作

我自己验证,不推荐插件


提前感谢。

问题在于元素引用,在循环中
元素
指的是添加新属性的jQuery对象,但在事件处理程序中
指的是没有添加新属性的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
    })

})
或者在事件处理程序中使用闭包变量
元素

_.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
    })

})

我认为解决方案是使用.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?
                })

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


您是否尝试将焦点事件放在循环外部。您应该将所有内容都放在函数中,然后像这样将其分配给元素:
$(“#myElement”).data('myFunction',createdFunctionName())
我相信这可能会起作用。但是使用loop会帮我省很多钱。但我没试过你想要的那一个say@AminJafari,我不清楚你的意思。你能给我一些详细的答案吗?@3gwebtrain你能试试
元素[0]吗?第一个示例中的errorMessage
。。。还有log
console.log(元素[0])
第二种方法似乎有效。我如何才能在foucs,blur…上添加侦听器。。?和其他方法一样?我如何请求调用,总是让我的元素成为启用状态?
_.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?
                })

})