javascript jQuery - 多个元素类型的每个循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9218000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:58:09  来源:igfitidea点击:

jQuery - each loop on multiple element types

javascriptjquery

提问by Stan

I've build pretty simple validator that will display my custom error message when someone tries to submit empty form. But I have some questions.

我已经构建了非常简单的验证器,当有人尝试提交空表单时,它将显示我的自定义错误消息。但我有一些问题。

I have .each()loop on :inputelements - how can I make it loop through :inputand textarea?

.each():input元素上有循环- 我怎样才能让它循环通过:inputtextarea

I use $(this).parent()to get the form object of input, but how can I make sure that it is form, not some other element like div?

$(this).parent()用来获取输入的表单对象,但是如何确保它是表单,而不是像 div 这样的其他元素?

Code with comments

带注释的代码

$('form input[type=submit]').click(function(){

    // First we get the form class
    var form = $(this).parent(); // How can I make sure that the form is selected, not some other parent like div?
    var formClass = $(form).attr('class');

    // Then remove every previous messages on this form
    $('.' + formClass + ' .validation-error').each(function(){
        $(this).remove();
    });

    // Iterate through every input to find data that needs to be validated
    $('.' + formClass + ' :input').each(function(){ // How can I make this work with textareas as well as inputs without copying this whole each loop?

        // If it is marked as required proceed
        if ($(this).attr('required') == 'required'){

            // Getting current text and placeholder text
            var currentText = $(this).val();
            var placeholderText = $(this).attr('placeholder');

            // Replacing spaces to avoid empty requests
            currentText = currentText.replace(' ', '');
            placeholderText = placeholderText.replace(' ', '');

            // If current text is empty or same as placeholder proceed
            if (currentText == '' || currentText == placeholderText){

                // Get input position in order to place error message
                var inputPos = $(this).position();
                var left = inputPos.left + 200;
                var top = inputPos.top - 4;

                // Generate error message container and error message itself
                var errorMsg = '<div class="validation-error" style="position:absolute;left:' + left + 'px;top:' + top + 'px;"><- This</div>';

                // Appending error message to parent - form
                $(this).parent().append(errorMsg);
            }
        }
    });
});

回答by Adam Hopkinson

Question 1:

问题 1:

I have .each() loop on :input elements - how can I make it loop through :input and textarea?

我在 :input 元素上有 .each() 循环 - 如何让它循环通过 :input 和 textarea?

Answer:

回答:

$(':input, textarea').

Question 2:

问题2:

I use $(this).parent() to get the form object of input, but how can I make sure that it is form, not some other element like div?

我使用 $(this).parent() 来获取输入的表单对象,但是如何确保它是表单,而不是像 div 这样的其他元素?

Answer:

回答:

$(this).closest('form')

回答by driangle

You can use a comma to select multiple elements:

您可以使用逗号来选择多个元素:

form.find("input,textarea").each(function(){...} );

回答by Roman Bataev

To iterate through both input and textarea:

遍历 input 和 textarea:

$('.' + formClass + ' :input, .' + formClass + ' textarea').each...

To access parent form:

要访问父表单:

$(this).parents("form")