Error with my script

-1
<div>
                    @foreach (var j in jornada)
                    {
                       <input class="filtro" name="NombrJornada" type="radio" value="@j.IdJornada"/>
                       @j.Nombre
                    }
 </div>

And my script

$(function () {
        $(".filtro").click(function () {
            var id = document.getElementsByClassName("filtro")[0].value;
            if (id == null)
                console.log("Prueba");
            else
                console.log(id);

        });
        function ErrorAjax(err) {
            alert(err.responseText);
        };
    });

But when I select a radio button I always get the id 1

They could help me

    
asked by JPerez 14.02.2018 в 00:29
source

1 answer

0

Try using this since you always select the first one

$(function () {
    $(".filtro").click(function () {
        var id = $(this).val();
        if (id == null)
            console.log("Prueba");
        else
            console.log("Radio "+ id + " Seleccionado");
    });
    function ErrorAjax(err) {
        alert(err.responseText);
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Radio 1</label>
<input class="filtro" name="NombrJornada" type="radio" value="1"/>
<br />
<label>Radio 2</label>
<input class="filtro" name="NombrJornada" type="radio" value="2"/>
    
answered by 14.02.2018 / 03:06
source