最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is $(this).val() giving me the value from this first method in this second method? - Stack Overflow

programmeradmin0浏览0评论

The $(this).val() in the second method returns the same value that I get in the first method. I expected to get the first value of the fields with the secondGroup class. What am I doing wrong?

$(document).ready(function(){

  jQuery.validator.addMethod("method1", function(value, element, options) {
        .....some code here....
  var elems = $(element).parents('form').find(options[0]);
      jQuery.each(elems, function(){ 
      thisVal = $(this).val();
      });
        .....some code here......
}, jQuery.format("some message."));

  jQuery.validator.addMethod("method2", function(value, element, options) {
        .....some code here....
  var elems = $(element).parents('form').find(options[0]);
      jQuery.each(elems, function(){ 
      thisVal = $(this).val();
      });
        .....some code here......
}, jQuery.format("some message."));


 $("#formName").validate({

 rules: {
   firstMethod1:{
       method1: ['.firstGroup']
   },
   secondMethod1:{
       method1: ['.firstGroup']
   },
   thirdMethod1:{
       method1: ['.firstGroup']
   },
   firstMethod2:{
       method2: ['.secondGroup']
   },
   secondMethod2:{
       method2: ['.secondGroup']
   },
   thirdMethod2:{
       method2: ['.secondGroup']
   }

   }
 });

});

The $(this).val() in the second method returns the same value that I get in the first method. I expected to get the first value of the fields with the secondGroup class. What am I doing wrong?

$(document).ready(function(){

  jQuery.validator.addMethod("method1", function(value, element, options) {
        .....some code here....
  var elems = $(element).parents('form').find(options[0]);
      jQuery.each(elems, function(){ 
      thisVal = $(this).val();
      });
        .....some code here......
}, jQuery.format("some message."));

  jQuery.validator.addMethod("method2", function(value, element, options) {
        .....some code here....
  var elems = $(element).parents('form').find(options[0]);
      jQuery.each(elems, function(){ 
      thisVal = $(this).val();
      });
        .....some code here......
}, jQuery.format("some message."));


 $("#formName").validate({

 rules: {
   firstMethod1:{
       method1: ['.firstGroup']
   },
   secondMethod1:{
       method1: ['.firstGroup']
   },
   thirdMethod1:{
       method1: ['.firstGroup']
   },
   firstMethod2:{
       method2: ['.secondGroup']
   },
   secondMethod2:{
       method2: ['.secondGroup']
   },
   thirdMethod2:{
       method2: ['.secondGroup']
   }

   }
 });

});
Share Improve this question edited Jun 13, 2011 at 18:04 coder asked Jun 13, 2011 at 17:53 codercoder 6,23319 gold badges57 silver badges73 bronze badges 3
  • 2 Doesn't var elems = $(element).parents('form').find(options[0]); that find the same form each time and just loop through the same values thus outputting the same each time? – rivenate247 Commented Jun 13, 2011 at 18:12
  • options[0] is different for each method. It has a value of '.firstGroup' or '.secondGroup'. – coder Commented Jun 13, 2011 at 18:58
  • 1 make a jsFiddle, please with the simplest example causing problem – kwicher Commented Jun 13, 2011 at 19:11
Add a ment  | 

1 Answer 1

Reset to default 3

You are using jQuery.each() instead of .each().

Use:

elems.each(function(){ 
    thisVal = $(this).val(); 
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论