简体   繁体   English

jQuery 选择器输入 [type=text]:nth-child(2) 不工作

[英]jQuery selector input[type=text]:nth-child(2) not working

I'm unable to use the ids or the class names associated with the inputs due to how they're randomly generated on input.我无法使用与输入相关的 ID 或 class 名称,因为它们是在输入时随机生成的。

The render looks like this:渲染看起来像这样:

<div class='dateSearch'>
  <label>Label 1</label>
  <input type='text' id='random_id_1'/>
  <span>Icon</span>
  <input type='text' id='random_id_2'/>
  <span>Icon</span>
  <input type='button' id='searchBtn'/>
</div>

I have no control of the render and can not change any content.我无法控制渲染,也无法更改任何内容。 So instead I was trying to grab the second text input and add a label before it.所以相反,我试图抓住第二个文本输入并在它之前添加一个 label 。

<script>
  $('<label>Label 2</label>').insertBefore('.dateSearch input[type=text]:nth-child(2)')
</script>

.dateSearch input[type=text] will add the label infront of both text inputs, but :nth-child(2) doesn't seem to want to work. .dateSearch input[type=text]将在两个文本输入前添加 label,但:nth-child(2)似乎不想工作。

I've tried .dateSearch > input:nth-child(2) and that failed to work as well.我试过.dateSearch > input:nth-child(2)也没有成功。 I just wanted to add a label before the second input element.我只是想在第二个输入元素之前添加一个 label 。

 $('<label>Label 2</label>').insertBefore('.dateSearch input[type=text]:nth-child(2)')
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> <div class='dateSearch'> <label>Label 1</label> <input type='text' id='random_id_1'/> <span>Icon</span> <input type='text' id='random_id_2'/> <span>Icon</span> <button type="button" class="btn btn-secondary">Search</button> </div>

Want it to look like this:希望它看起来像这样:

Label_1 [ Input Text ] ( Icon ) Label_2 [ Input Text ] ( Icon ) [ Button ] Label_1 [输入文本](图标Label_2 [输入文本](图标)[按钮]

Are you familiar with eq() ?你熟悉eq()吗? That gets you around the child selector problem because you can target elements by type as well.这可以解决子选择器问题,因为您也可以按类型定位元素。

Don't forget to put a for attribute on the label for accessibility if you can.如果可以的话,不要忘记在 label 上添加一个for属性以方便访问。

 const secondInput = $('.dateSearch input[type=text]').eq(1); // zero-based const secondInputId = secondInput.attr('id'); // optional, but wise const label = $('<label>Label 2</label>'); label.attr('for', secondInputId); // optional, but wise label.insertBefore(secondInput);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class='dateSearch'> <label>Label 1</label> <input type='text' id='random_id_1' /> <span>Icon</span> <input type='text' id='random_id_2' /> <span>Icon</span> <button type="button" class="btn btn-secondary">Search</button> </div>

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

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