jquery与jquery的兼容性验证旧版本(jquery compatibility with jquery validate older versions)

编程入门 行业动态 更新时间:2024-10-25 05:18:18
jquery与jquery的兼容性验证旧版本(jquery compatibility with jquery validate older versions)

我正在使用jquery验证v1.7和jQuery v1.4.2,我必须在blur和onkeyup事件上使用.live。 这些事件似乎不适用于这些版本。 有关如何使用这些约束处理onkeyup和模糊事件的任何解决方案都会有所帮助。

注意:我将无法将这些版本更新为可用的最新版本,因为多个团队正在使用这些文件的旧版本,并且他们不想升级到最新版本。

Jquery验证调用:

onkeyup: function(element){ C.options.throwMessages(element);

},

throwMessages: function(el){ if($(el).attr('name') === "email"){ //Validating email field var selectedField = $(el); var emailValid = C.options.simple_validate_email(selectedField); if (C.options.pageName === 'login' || C.options.pageName === 'register'){ $(C.options.currentPage).find(':submit').click(function(){ var selectedField = $(C.options.currentPage).find('input.email'); if(!selectedField.val().length){ selectedField.removeClass('errorIndicator'); } }); } if(C.options.pageName === "register"){ selectedField.live('blur', function(){ if($(this).val().length === 0){ $(this).parent().parent().removeClass('aimValid'); } }); selectedField.live('keydown focusout', function(event) { if(event.type == 'keydown'){ if(!C.options.simple_validate_email($(this))){ $(this).parent().parent().removeClass('aimValid'); $(C.options.currentPage).find('.emailTip').show().html('Please enter a valid email address.'); }else{ $(this).removeClass('errorIndicator'); $(C.options.currentPage).find('.emailTip').hide().html(''); } }else if(event.type == 'focusout'){ if(!C.options.simple_validate_email($(this))){ }else{ $(this).removeClass('errorIndicator'); $(C.options.currentPage).find('.emailTip').hide().html(''); } } }); } }else if($(el).attr('name') === "password"){//Validating password field var selectedPasswordField = $(el); if(C.options.pageName === "register"){ //password field selectedPasswordField.live('keydown', function(){ if($(this).val().length === 0){ $(this).parent().parent().removeClass('aimValid'); $(this).removeClass('aimError'); } }); } }else if($(el).attr('name') === "username2"){//Validating email field on ForgotPassword screen var selectedField = $(el); if (C.options.pageName === 'forgotPassword') { var isFormValid = false; $(C.options.currentPage).find(':submit').click(function(){ if((selectedField.val().length === 0) || !selectedField.parent().parent().find('div.aimError').length){ isFormValid = true; } selectedField.live('keydown focusout', function(event){ if(event.type === 'keydown'){ $(C.options.currentPage).find('.emailTip').hide().html(''); }else{ $(C.options.currentPage).find('.emailTip').hide().html(''); } }); if (isFormValid) { $(C.options.currentPage).find('.emailTip').show().html('Please enter a valid email address.'); }else{ $(C.options.currentPage).find('.emailTip').hide().html(''); } }); } } },

I'm using jquery validate v1.7 and jQuery v1.4.2 and I have to use .live on blur and onkeyup events. These events dont seem to work with these versions. Any solution on how to deal with onkeyup and blur events with these constraints would be helpful.

Note:I won't be able to update these versions to the latest versions available because multiple teams are using the old versions of these files and they dont want to upgrade to the latest.

Jquery validate call:

onkeyup: function(element){ C.options.throwMessages(element);

},

throwMessages: function(el){ if($(el).attr('name') === "email"){ //Validating email field var selectedField = $(el); var emailValid = C.options.simple_validate_email(selectedField); if (C.options.pageName === 'login' || C.options.pageName === 'register'){ $(C.options.currentPage).find(':submit').click(function(){ var selectedField = $(C.options.currentPage).find('input.email'); if(!selectedField.val().length){ selectedField.removeClass('errorIndicator'); } }); } if(C.options.pageName === "register"){ selectedField.live('blur', function(){ if($(this).val().length === 0){ $(this).parent().parent().removeClass('aimValid'); } }); selectedField.live('keydown focusout', function(event) { if(event.type == 'keydown'){ if(!C.options.simple_validate_email($(this))){ $(this).parent().parent().removeClass('aimValid'); $(C.options.currentPage).find('.emailTip').show().html('Please enter a valid email address.'); }else{ $(this).removeClass('errorIndicator'); $(C.options.currentPage).find('.emailTip').hide().html(''); } }else if(event.type == 'focusout'){ if(!C.options.simple_validate_email($(this))){ }else{ $(this).removeClass('errorIndicator'); $(C.options.currentPage).find('.emailTip').hide().html(''); } } }); } }else if($(el).attr('name') === "password"){//Validating password field var selectedPasswordField = $(el); if(C.options.pageName === "register"){ //password field selectedPasswordField.live('keydown', function(){ if($(this).val().length === 0){ $(this).parent().parent().removeClass('aimValid'); $(this).removeClass('aimError'); } }); } }else if($(el).attr('name') === "username2"){//Validating email field on ForgotPassword screen var selectedField = $(el); if (C.options.pageName === 'forgotPassword') { var isFormValid = false; $(C.options.currentPage).find(':submit').click(function(){ if((selectedField.val().length === 0) || !selectedField.parent().parent().find('div.aimError').length){ isFormValid = true; } selectedField.live('keydown focusout', function(event){ if(event.type === 'keydown'){ $(C.options.currentPage).find('.emailTip').hide().html(''); }else{ $(C.options.currentPage).find('.emailTip').hide().html(''); } }); if (isFormValid) { $(C.options.currentPage).find('.emailTip').show().html('Please enter a valid email address.'); }else{ $(C.options.currentPage).find('.emailTip').hide().html(''); } }); } } },

最满意答案

.live()已在所有版本的jQuery中弃用。 对于1.4,您可以像这样使用.delegate() :

$("#container").delegate("#test", "keyup keydown focusout", function(e) { log(e.type + " event"); });

工作演示: http : //jsfiddle.net/jfriend00/WxqwS/


对于1.7,您可以使用.on() ,它与.delegate()几乎相同,除了参数更符合逻辑顺序:

$("#container").on("keyup keydown focusout", "#test", function(e) { log(e.type + " event"); });

工作演示: http : //jsfiddle.net/jfriend00/FULfe/

.live() has been deprecated in all versions of jQuery. For 1.4, you can use .delegate() like this:

$("#container").delegate("#test", "keyup keydown focusout", function(e) { log(e.type + " event"); });

Working demo: http://jsfiddle.net/jfriend00/WxqwS/


For 1.7, you can use .on() which is pretty much the same as .delegate() except the arguments are in a bit more logical order:

$("#container").on("keyup keydown focusout", "#test", function(e) { log(e.type + " event"); });

Working demo: http://jsfiddle.net/jfriend00/FULfe/

更多推荐

本文发布于:2023-08-07 19:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:兼容性   旧版本   jquery   compatibility   versions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!