javascript - 在 <a> 之前的多个元素之前搜索 <select>

标签 javascript jquery html

如何搜索 select <a> 之前的多个元素之前的元素?

我知道$('select[name=foo]').val())

    <select name="foo">
      <option value="1">1</option>
      <option value="2">3</option>
      <option value="2">3</option>
    </select>

    <div class="foo2"></div>
    <p>Lorem Ipsum</p>

    <a class="button" href="#">Search</a>

这是我的尝试:

$('a').click(function(e){
  e.preventDefault();
  console.log($(this).prevUntil('select'));
});

最佳答案

使用prevUntil将为您提供选择器中元素之前的所有元素

来自文档:

Description: Get all preceding siblings of each element up to but not including the element matched by the selector, DOM node, or jQuery object.

您想使用 prevAll('select:first') 获取前一个选择 http://api.jquery.com/prevall/

Description: Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.

$(this).prevAll('select:first')

FIDDLE

关于javascript - 在 <a> 之前的多个元素之前搜索 <select>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25087538/

相关文章:

javascript - JSON对象数组不为空但无法使用foreach迭代,显示零长度

html - 内容旁边显示的额外空间

jquery - 删除输入字段的部分值

javascript - 使用Javascript确定 'Overtype'模式

javascript - React-Redux 如何知道要传递什么存储片段?

javascript - 测试表单数据并返回单个错误消息(Javascript)

javascript - 可在 jQuery 插件外部访问的对象

javascript - 在javascript中将数组(对象?)转换为字符串

c# - 如何使用 Javascript 或 JQuery 获取页面内容

html - Bootstrap : How to center align content inside column?