我需要以下 jQuery 的帮助,它对我不起作用。我只想在 <optgroup label="Knowledge Base">
时触发一些操作不存在
我的 HTML(无法更改):
<select id="entry_forum_id" name="entry[forum_id]">
<optgroup label="Knowledge Base"> … </optgroup>
<optgroup label="Incidents"> … </optgroup>
<optgroup label="Articles"> … </optgroup>
</select>
我的 jQuery 尝试:
if($('optgroup:not([label="Knowledge Base"])')) {
alert('test');
}
最佳答案
$('optgroup:not([label="Knowledge Base"])')
返回一个 jQuery 包装对象,您需要测试它的 length 属性以检查它是否包含任何元素
你需要测试
if($('optgroup[label="Knowledge Base"]').length == 0) {
alert('test');
}
关于Jquery,如果 optgroup 不包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244848/