Last element in .each() set

后端 未结 3 480
陌清茗
陌清茗 2020-12-02 22:14

I have an issue, where by I am doing a simple form validation, that needs some custom stuff that the validation plugin could not do for me. Basically, I have a name, email a

3条回答
  •  死守一世寂寞
    2020-12-02 22:32

    each passes into your function index and element. Check index against the length of the set and you're good to go:

    var set = $('.requiredText');
    var length = set.length;
    set.each(function(index, element) {
          thisVal = $(this).val();
          if(parseInt(thisVal) !== 0) {
              console.log('Valid Field: ' + thisVal);
              if (index === (length - 1)) {
                  console.log('Last field, submit form here');
              }
          }
    });
    

提交回复
热议问题