简体   繁体   English

我想在单击跨度时找到最接近跨度的输入元素,你能帮我吗?

[英]I want to find an input element closest a span when I click the span, can you help me?

I want to find an input element closest a span when i click the span当我单击跨度时,我想找到最接近跨度的输入元素

HTML HTML

<div>
   <span class="text"></span>
   <input type="text" name="an_element_name">
</div>
<div>
   <span class="text"></span>
   <input type="text" name="an_element_name">
</div>

JQuery查询

$('.text').click(function(e){
   let text = $(e.target),
       input = text.closest('input[name=an_element_name]').find('input[name=an_element_name]');

   input.val('some value');
});

but, I'm still can't set a value of the input.但是,我仍然无法设置输入值。 Can you help me ?你能帮助我吗 ?

You need to use text.next('input[name=an_element_name]');您需要使用text.next('input[name=an_element_name]'); since the .closest() is looking for an ancestor of your span, not siblings.因为.closest()正在寻找您的跨度的祖先,而不是兄弟姐妹。

  • .closest() = Goes up the html tree and search for the element. .closest() = 向上 html 树搜索元素。
  • .next() = Searches for an element after the current element. .next() = 在当前元素之后搜索一个元素。
  • .find() = Searches for children of the given element. .find() = 搜索给定元素的子元素。

Demo演示

 $('.text').click(function(e){ let text = $(e.target), input = text.next('input[name=an_element_name]'); input.val('some value'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="text">click</span> <input type="text" name="an_element_name">

You are using closest() incorrectly, you should use next() instead.您错误地使用了nearest() ,您应该使用next()来代替。

next(): Get the immediately following sibling of each element in the set of matched elements. next():获取匹配元素集中每个元素的紧跟其后的兄弟元素。 If a selector is provided, it retrieves the next sibling only if it matches that selector.如果提供了选择器,则仅当它与该选择器匹配时才检索下一个兄弟。

You can achieve it in this simple way您可以通过这种简单的方式实现它

 $('.text').click(function(e){ let text = $(e.target), input = text.next('input[name=an_element_name]'); input.val('some value'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="text">Click me</span> <input type="text" name="an_element_name">

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

相关问题 当我单击下面给出的代码中的计算按钮时,有人可以帮助我如何更改添加到输入框中的伪跨度的颜色 - Can someone help me how to change the color of pseudo span added to the input box when I click on calculate button in code given below 我怎样才能找到最接近的跨度? - how can i find closest span? 单击跨度时,如何获取由跨度标签括起来的文本并将其保存为隐藏的输入值? - How can get the text enclosed by a span tag and save it to a hidden input value when I click the span? 我想在单击 btn 时将样式(特定背景颜色)的元素(跨度或 div)创建到父元素中? - i want to create element (span or div) with style( specific background-color) into parent element when click on btn? 单击子范围中的X标记时如何隐藏元素 - How to hide an element when I click the X mark in a child span 查找最近的span元素并更改CSS类 - Find closest span element and change the CSS Class 我怎样才能让jQuery DatePicker不打断<span>或</span> <span><input></span> <span>元素何时被调用?</span> - How can I get jQuery DatePicker to not break a <span> or <input> element when called? 查找最接近的跨度文字? - Find closest span text? 如何在表单中引用span元素? - How can i refer to a span element in a form? 如何更改<span>元素</span>的文本<span>?</span> - How can I change the text of a <span> element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM