Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   подскажите с :has, :first-child и :not(:disabled) (https://javascript.ru/forum/jquery/3315-podskazhite-s-has-first-child-i-not-disabled.html)

xpg934 06.04.2009 18:49

подскажите с :has, :first-child и :not(:disabled)
 
Что-то не пойму, в чем проблема.
Есть табличка. Задача: выделить те строки, у которых в _первой_ ячейке есть checked чекбокс, и при этом он не должен быть disabled.

Код тут:

<html>
<body>
    <script type="text/javascript" src="jquery-1.3.2.js"></script>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $("table tr:has(td:first-child > :checkbox:checked:not(:disabled))")
                .css("background-color", "red");
        });
    </script>

    <table>
        <tr><td><input type="checkbox" value="true" /></td><td>Item 1</td><td>Item 2</td></tr>
        <tr><td><input type="checkbox" value="true" checked="true"/></td><td>Item 1</td><td>Item 2</td></tr>
        <tr><td><input type="checkbox" value="true" /></td><td>Item 1</td><td><input type="checkbox" value="true" checked="true"/></td></tr>
        <tr><td><input type="checkbox" value="true" checked="true" disabled="true"/></td><td>Item 1</td><td>Item 2</td></tr>
        <tr><td><input type="checkbox" value="true" /></td><td>Item 1</td><td>Item 2</td></tr>
        <tr><td><input type="checkbox" value="true" checked="true"/></td><td>Item 1</td><td>Item 2</td></tr>
    </table>
</body>

</html>


В результате ничего вообще не выделяется. Решить проблему можно например убрав first-child:
$("table tr:has(td > :checkbox:checked:not(:disabled))")

но тогда в выбор попадает строка, в которой чекбокс не в первой колонке

Второе решение, убрать проверку на disabled:
$("table tr:has(td:first-child > :checkbox:checked)")

тогда всё правильно, но не проверяется на disabled

Третий вариант, работающий:
$("table tr:has(td:first-child > :checkbox:checked)").not(":has(td:first-child > :checkbox:disabled)")

так всё работает, как задано задачей... но, ПОЧЕМУ не работает первый вариант?

e1f 23.04.2009 16:09

1. При использовании jquery версии 1.2.6 Ваш первоначальный пример работает.
2. А вот почему не работает в 1.3.2... Что-то изменили в механизме выбора, почитайте, что изменилось в версии 1.3.
Можно написать одним селекторм в Вашем случае:
$("table tr:has(td:first-child > :checkbox:checked[disabled!='true'])").css("background-color", "red");

e1f 23.04.2009 16:17

Хе-хе... все проще:
$("table tr:has(td:first-child > :checkbox:checked:enabled)")


Часовой пояс GMT +3, время: 18:09.