.each() 集中的最后一个元素

发布于 2024-09-28 11:48:10 字数 1075 浏览 5 评论 0原文

我有一个问题,我正在做一个简单的表单验证,需要一些验证插件无法为我做的自定义内容。基本上,我有一个姓名、电子邮件和消息字段,所有这些都是必需的,但只有电子邮件是真正经过验证的,其他只需要检查它们是否不为空。这是我当前的代码:

$("#contactMe form").submit(function() {
    var email = $('.requiredEmail').val();
    if(email != 0)  {
        if(isValidEmailAddress(email))  {
            $('.requiredText').each(function() {
                thisVal = $(this).val();
                var $this = $(this);
                if(thisVal != 0) {
                    console.log('Valid Field: '+thisVal);
                    if ($(this) == $(this).parent(':last')) {
                        console.log('Last field, submit form here');
                    }
                }
            });
        } else {
            console.log('Email Not Valid');
        }
    } 
    return false;
});

为了解释一下,我首先通过 isValidEmailAddress 函数检查电子邮件地址是否有效,该函数正在运行。然后我将使用each()、所有必需的文本字段并检查它们是否不为空。当我到达最后一个 requiredText 字段时,我想使用 post 或其他方式提交表单。

if ($(this) == $(this).parent(':last')) { 我所拥有的显然是不正确的,但我不确定可以用什么来检查它是否正确每个结果集中的最后一个,如果为真则执行操作。

有人可以帮我吗?

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 and message field, all are required, but only email is really validated, the others just need to check if theyre not empty. Here is my current code:

$("#contactMe form").submit(function() {
    var email = $('.requiredEmail').val();
    if(email != 0)  {
        if(isValidEmailAddress(email))  {
            $('.requiredText').each(function() {
                thisVal = $(this).val();
                var $this = $(this);
                if(thisVal != 0) {
                    console.log('Valid Field: '+thisVal);
                    if ($(this) == $(this).parent(':last')) {
                        console.log('Last field, submit form here');
                    }
                }
            });
        } else {
            console.log('Email Not Valid');
        }
    } 
    return false;
});

Just to explain, I am first checking the email address is valid via the isValidEmailAddress function, which is working. Then I am going through using each(), all the requiredText fields and checking if theyre not empty. When I get to the last requiredText field, I want to submit the form using post or whatever.

if ($(this) == $(this).parent(':last')) { What I have there is obviously incorrect but I am not sure what can be used to check if it is the last in the each result set, and perform an action if true.

Can anybody help me out here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒数 2024-10-05 11:48:10

each 传递到您的函数 index 和 <代码>元素。检查 index 与集合的长度,然后就可以开始了:

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');
          }
      }
});

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');
          }
      }
});
屋顶上的小猫咪 2024-10-05 11:48:10

对于未来的 Google 员工,我有一种不同的方法来检查它是否是最后一个元素。它类似于OP问题中的最后几行。

这直接比较元素而不仅仅是检查索引号。

$yourset.each(function() {
    var $this = $(this);
    if($this[0] === $yourset.last()[0]) {
        //$this is the last one
    }
});

For future Googlers i've a different approach to check if it's last element. It's similar to last lines in OP question.

This directly compares elements rather than just checking index numbers.

$yourset.each(function() {
    var $this = $(this);
    if($this[0] === $yourset.last()[0]) {
        //$this is the last one
    }
});
烙印 2024-10-05 11:48:10

来自此处的简短答案,针对此问题进行了调整:

var arr = $('.requiredText');
arr.each(function(index, item) {
   var is_last_item = (index == (arr.length - 1));
});

只是为了完整性。

A shorter answer from here, adapted to this question:

var arr = $('.requiredText');
arr.each(function(index, item) {
   var is_last_item = (index == (arr.length - 1));
});

Just for completeness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文