代码:
<select id="avoid_id">
<option value="All">All</option>
</select>
<select>
<option value="All">All</option>
</select>
<select>
<option value="All">All</option>
</select>
<select>
<option value="All">All</option>
</select>
jQuery:
$('select :not(avoid_id)').change(some_function); // not working
我想对所有 <select>
执行相同的功能标签更改,除了 id avoid_id
.
如何实现这一目标?
最佳答案
您需要使用 $('select:not(#avoid_id)')
因为 :not
是一个 css 选择器,它与元素一起使用,因此使用 select:not
并且 :not()
的参数是 id 值,因此在其中使用 #avoid_id
:
$('select:not(#avoid_id)').change(some_function);
function some_function() {
console.log('changed');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<select id="avoid_id">
<option value="All">All</option>
<option value="1">1</option>
</select>
<select>
<option value="All">All</option>
<option value="1">1</option>
</select>
<select>
<option value="All">All</option>
<option value="1">1</option>
</select>
<select>
<option value="All">All</option>
<option value="1">1</option>
</select>
关于javascript - jQuery:选择需要一个 id 的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59859944/