4

以下の 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');
}
4

2 に答える 2

6

$('optgroup:not([label="Knowledge Base"])') returns a jQuery wrapper object, you need to test its length property to check whether it contains any elements

You need to test

if($('optgroup[label="Knowledge Base"]').length == 0) {
    alert('test');
}
于 2013-08-15T01:15:37.347 に答える
5

I don't think you need the pseudo-selector :not. Just check there are no optgroup with that label:

if($('optgroup[label="Knowledge Base"]').length == 0) {

Fiddle.

于 2013-08-15T01:16:10.080 に答える