Doubt to take the name of an automatically generated php input field

Asked

Viewed 88 times

0

I have a legacy system in php that generates the input radios, and the names of these fields are dynamic, such as the $Row object. And now I need to take the values of the radio fields and their respective names so that they are saved in the bank through a requisicao ajax. Take the example:

<?php
   $row = new stdClass();
   $row->sigla = array('ag', 'bg', 'cg');
   foreach ($row->sigla as $r) {
       echo "<tr>
                <td style='text-align:center'><input type='radio' value='1' name='$r' class='avaliacao'>SIM</td>
                <td style='text-align:center'><input type='radio' value='2' name='$r' class='avaliacao'>NAO</td>
                <td style='text-align:center'><input type='radio' value='3' name='$r' class='avaliacao'>N/A</td>
            </tr><br/>";
          }
        ?>

And the js I’m trying to run, but not knowing how to get the name of the field:

$(document).ready(function(){
           $("input[type='radio']").click(function() {
                var value = $(this).val();//aqui retorna o valor correto
                //AQUI gostaria de pegar o valor do que foi clicado, mas como pegar o nome do campo?
            });
 });

1 answer

2

Thus:

$(document).ready(function(){
    $("input[type='radio']").click(function() {
        var value = $(this).val();//aqui retorna o valor correto
        var nome = $(this).attr("name");
    });
});
  • easy so kkkkkk

  • Didn’t work out ?

  • gave yes, very easy, I need to study more js. Thank you

Browser other questions tagged

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