How to use $(this) + checked?

Asked

Viewed 1,064 times

3

<input type="radio" name="teste1" value="SIM"> TESTE 1
<input type="radio" name="teste1" value="NÃO"> TESTE 2



$(document).ready(function(){
    $("[name='teste1']").click(function () {
        var teste = $(this+":checked").val();
        alert(teste);

    });
});    

How to get the value using $(this) + checked?

    if($("[name='teste1']").is(":checked")){
        var teste = $(this+":checked").val();
        alert(teste);
    }

is making a mistake: http://jsfiddle.net/hv2wc25v/

I wonder how you could use the dial :checked , when the this is already the selector.

  • @Wallace, as it is radio only one element will be selected, so there is no need to check which one is checked, by the fact that you already set that in the click this will be set. so just $(this). val(); would solve your problem, or you could show me a case you wouldn’t solve?

  • 1

    @Gabrielrodrigues, you’re all set. Maybe in very specific cases will give problems, but see my answer, for example, I did using only the this, as you have demonstrated. But I am using the method change, and not the click. I think it’s more appropriate

  • If you wanted to trigger a function every time someone changes the radio change would be the best option, why on click if you continue to click the same radio already checked it would continue to trigger the function.

  • Denali, does any answer solve your problem? It is interesting to give feedback, because someone else may have the same problem, and when you get here you will know what was done. : ) See here what you can do by getting an answer to your question. ;)

2 answers

5

There is also another way, which is using the function filter.

Thus:

 $(function (){
      
      $("[name='teste1']").click(function () {
         alert($(this).filter(':checked').val());
      });
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="radio" name="teste1" value="SIM"> TESTE 1
<input type="radio" name="teste1" value="NÃO"> TESTE 2

The first case can fit very well, but you can still do using the function change, since this is the current element on which the event was fired.

Use the function change:

 $(function (){

      $("[name='teste1']").change(function () {
         alert($(this).val());
      });
    })

Note: With change, it will only be triggered when there is an alternation between values. That is, clicking an already marked checkbox, there will be no activities captured by change.

4

Try it this way:

$(this).is(":checked")

Something I also like to do, particularly, is to assign everything in variavies, so:

var $myRadio = $("#idDoRadio");

$myRadio.on('click', function () {
    if ($(this).is(":checked")) {
          alert($(this).val())
    }
});
  • 1

    $(this). is(":checked"). val() wanted to get the value, it seems that it did not work.

  • 1

    Check if this resolves: http://jsfiddle.net/hv2wc25v/2/

  • I think @Wallace’s answer solves.

  • Come on. Watching well the answer, is(':checked') does not return the jQuery object. and yes, boolean. To do what he needs, I’ll have to edit the answer.

  • Yes, at first he had not described that he needed to get the value of the radio checked (or I did not pay attention), the way I explained it serves only to validate whether or not it is checked. But after checking it is possible to access the value using $(this). val()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.