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