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?
– Gabriel Rodrigues
@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 methodchange
, and not theclick
. I think it’s more appropriate– Wallace Maxters
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.
– Gabriel Rodrigues
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. ;)
– Pedro Camara Junior