单击时检查表单中是否有显示 none 的元素

On click check if there are elements in a form which are with display none

我有一个表单,其中我在单击按钮时隐藏了一些输入。我有一个验证码,它贯穿每个输入并在输入为空时打印一条错误消息。但是为了提交表单,我必须填写每个元素,包括不可见的元素,因为代码正在打印消息,尽管我已经填写了所有可见的元素。

这是验证代码

$('button.submit-first').click(function() {
    var emptyElements = $('form.registration-form div.first-part :input').filter( function() {
        return this.value === '';
    });

    if (emptyElements.length === 0)
    {
        $('p.red').css('display', 'none');
    } 

    else
    {
        $('p.red').css('display', 'block');
        $('html, body').animate({scrollTop: $('div.row').offset().top}, 800);
    }
});

我似乎无法弄清楚我应该如何检查输入,如果有不可见的,请跳过它们并检查可见的。

您可以只在输入中添加“:visible”以仅验证那些。

$('button.submit-first').click(function() {
    var emptyElements = $('form.registration-form div.first-part :input:visible').filter( function() {
    return this.value === '';
});