使用 jquery find() 按类选择输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11058432/
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-08-26 10:08:03  来源:igfitidea点击:

Using jquery find() to select input by class

jqueryformsclassinputfind

提问by JellyFishBoy

I have a script which validates each stage of a form and adds a tick or cross to each section if the validation is without errors or not.

我有一个脚本,它验证表单的每个阶段,并在验证没有错误的情况下为每个部分添加一个勾号或叉号。

Here is my code:

这是我的代码:

var error = 1;
var hasError = false;
$('.edit_artist').children(':nth-child(' + parseInt(step) + ')').find('input:not(button)').each(function() {
                var $this = $(this);
                var valueLength = jQuery.trim($this.val()).length;
                if(valueLength == '') {
                    hasError = true;    
                }

I dont want the validation to occur on all input fields, so I want to be able to validate inputs by class. Is there a way of finding inputs by class or would I need to go down another route to achieve this?

我不希望在所有输入字段上都进行验证,所以我希望能够按类验证输入。有没有办法按班级找到输入,或者我需要走另一条路来实现这一目标?

Thanks

谢谢

回答by David says reinstate Monica

To find an element based on its class, and while excluding buttonelements:

根据class, 并排除button元素来查找元素:

$(selector).find('input.className:not("button")');

Or, a more verbose manner:

或者,更详细的方式:

$(selector).find('input:not("button")').filter(
     function(){
         return $(this).hasClass('className')
     });

References:

参考: