Ajuda check e uncheck

4 views
Skip to first unread message

Leonardo Caldeira

unread,
Oct 12, 2017, 9:09:40 PM10/12/17
to jQuery (Brasil)
Olá amigos, estou com um problema em meu codigo e gostaria de uma ajuda de vcs, sou novo em jquery.
então tudo ia bem e está perfeito, sendo que meu sistema tipo rating, nao consigo fazer ele executar o uncheck.
 eu quero que ele selecione ou deselecione se clicado novamente. somente isso.

segue abaixo o codigo:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>Pontos</title>
        <meta name="viewport" content="width=device-width">
        
        <style>
            .rating {
                overflow: hidden;
                display: inline-block;
                position: absolute;
                font-size:18px;
                color: #000000;
            }
            .rating-star {
                padding: 0 1px;
                margin: 0;
                cursor: pointer;
                display: block;
                float: right;
            }
            .rating-star:after {
                position: relative;
                font-family: FontAwesome;
                content:'\f1db';
            }
           
            .rating-star.checked ~ .rating-star:after,
            .rating-star.checked:after {
                content:'\f111';
            }
            
            .rating:hover .rating-star:after {content:'\f1db';}
            
            .rating-star:hover ~ .rating-star:after, 
            .rating-star:hover:after {
                content:'\f111' !important;
            }
            
            
            /* Just for the demo */
            body {
                margin: 20px;
            }
        </style>
    </head>
    <body>
        <div class="rating">
            <span class="rating-star" data-value="5"></span>
            <span class="rating-star" data-value="4"></span>
            <span class="rating-star" data-value="3"></span>
            <span class="rating-star" data-value="2"></span>
            <span class="rating-star" data-value="1"></span>
        </div>
      
        <script>
        $('.rating-star').click(function() {
            $(this).parents('.rating').find('.rating-star').removeClass('checked');
            $(this).addClass('checked');

        });
        </script>
    </body>
</html>

Rafael Sirotheau

unread,
Oct 16, 2017, 1:14:11 PM10/16/17
to jquery-br
Olá!

Primeira dica, não utilize o .parents() pra esse caso. Na verdade, os casos em que se utiliza o .parents() são raros.

Você pode substituir:
$(this).parents('.rating').find('.rating-star').removeClass('checked');
por
$(this).siblings().removeClass('checked');

E pra fazer o que queres, bastar trocar o addClass por toggleClass.


Abs

--
--
Grupo de jQuery Brasil.
 
REGRAS: https://docs.google.com/document/d/1VfvTDmeHTSj-L5ouZJjQQ4OkjTCU1Hr2XtPYBNGVHH4/edit
 
USE O JSBIN.COM / JSFIDDLE.NET PARA CÓDIGOS.
 
Email: jque...@googlegroups.com
Biba: jquery-br+unsubscribe@googlegroups.com
Site: http://groups.google.com/group/jquery-br?hl=pt-BR

---
Você recebeu essa mensagem porque está inscrito no grupo "jQuery (Brasil)" dos Grupos do Google.
Para cancelar inscrição nesse grupo e parar de receber e-mails dele, envie um e-mail para jquery-br+unsubscribe@googlegroups.com.
Para mais opções, acesse https://groups.google.com/d/optout.



--
Linkedin

Front-end Developer
+55 (11) 9 6452 1384

Reply all
Reply to author
Forward
0 new messages