简体   繁体   English

遍历右jquery选择器的dom元素

[英]traversing dom elements for right jquery selector

Given the following html snippet, I cant seem to access the input elements under the div with class="input-group" 鉴于以下html片段,我似乎无法使用class =“input-group”访问div下的输入元素

 <form action="login.html#register" method="post" id="form-register" class="form-horizontal display-none">
        <div class="form-group">
            <div class="col-xs-6">
                <div class="input-group">
                    <span class="input-group-addon"><i class="gi gi-user"></i></span>
                    <input type="text" id="register-firstname" name="register-firstname" class="form-control input-lg" placeholder="Firstname">
                </div>
           </div>
           <div class="col-xs-6">
               <input type="text" id="register-lastname" name="register-lastname" class="form-control input-lg" placeholder="Lastname">
           </div>
       </div>
       <div class="form-group">
           <div class="col-xs-12">
               <div class="input-group">
                   <span class="input-group-addon"><i class="gi gi-envelope"></i></span>
                   <input type="text" id="register-email" name="register-email" class="form-control input-lg" placeholder="Email">
               </div>
          </div>
     </div>
</form>

I have tried numerous selector syntax but nothing seems to work. 我尝试了很多选择器语法,但似乎没有任何工作。 I cannot figure out why this version doesn't work, it certainly should, but I'm no expert on JQuery just yet. 我无法弄清楚为什么这个版本不起作用,它当然应该,但我还不是JQuery的专家。 To me the syntax below says get all the items with class form-group in the element with id form-register, then get all divs with class input-group and then all the input tags in those divs. 对我来说,下面的语法是在带有id form-register的元素中获取带有class form-group的所有项,然后获取所有带有class input-group的div,然后获取这些div中的所有输入标记。 Am I mistaken there? 我错了吗?

 $('#form-register.form-group > div.input-group > input').attr('disabled', true);

The proper syntax is, via assistance from the accepted answer; 正确的语法是通过接受的答案的帮助;

 $('#form-register div[class^="col-xs"] input').attr('disabled', true);

Your CSS syntax is incorrect. 您的CSS语法不正确。 Try this: 尝试这个:

$('#form-register .form-group div.input-group > input').attr('disabled', true);

Firstly, notice the space between the first two selectors, because .form-group is a child of #form-register . 首先,注意前两个选择器之间的空间,因为.form-group#form-register的子.form-group Secondly the > means 'direct child of', so was incorrect when used on div.input-group . 其次, >表示'直接子',因此在div.input-group上使用时不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM