使用 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
Using jquery find() to select input by class
提问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 button
elements:
根据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:
参考: