0

I have a question regarding the selector.

I want to select the input field that is not under certain class.

My html

<div class='test'>
   ……
   <input type='text'></input>
 // more input field 
     <div class='nonTarget>
        <input type='text' ></input>
     <div>

</div>

I want to input field under test class but not nonTarget div.

I have

  var test = $('.test :not(.answer) input');
         test.each(function(){
             console.log($(this).val())
         })

but it seems select every input fields.. Can anyone help me about it? Thanks a lot!

4

2 回答 2

2

您可以使用$('.test input:not(.nonTarget input)').

它使用:not选择器,并找到所有inputs的后代.test,但不匹配选择器.nonTarget input(即不是后代.nonTarget)。

看这里;http://jsfiddle.net/LAgkX/

于 2013-08-21T21:38:09.227 回答
1

由于您使用的是 jQuery:

$('.test').children('input').each(function(){
    console.log($(this).val());
});

选择.children()器只在 DOM 树下走一步,所以你应该很好。

于 2013-08-21T21:36:59.423 回答