How to get the text of a radio button?

2

In my code I check the selected radio and would like to get the radio text field as well. How should I do it?!

No while it mounts the radius according to the values found in the table and shows the name of the fields found.

<?
    while($w_registro = pg_fetch_object($w_queryresultado))
      {                                   
      print('<input type="radio" name="rb_peri" id="id_rb_peri" value="'.$seq_cara.'">');
      print($tx_desc);
      }
?>

Then when the radio is selected and a "GO" is issued, it calls the function to check the selected radio.

function radio(){
    w_qtde_rb = document.forms['sai_frm_alte_novo_cara'].rb_peri.length;

    for  (w_i=0; w_i < w_qtde_rb; w_i++)
        {           
         if(document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].checked == true)
            {   
            get_valor= document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].value;
            }     
        }
}

Here I would like to get the field name for the selected radio. Any idea?

    
asked by anonymous 02.09.2014 / 19:39

2 answers

3

If you make a small change in your PHP, so that the text of the radiobutton is printed inside a tag (for example, <label> , which is good, in terms of usability), just add to your JavaScript highlighted line:

function radio(){
    w_qtde_rb = document.forms['sai_frm_alte_novo_cara'].rb_peri.length;

    for  (w_i=0; w_i < w_qtde_rb; w_i++)
    {           
        if(document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].checked == true)
        {   
            get_valor = document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].value;

            // Adicionar esta linha:
            get_texto = document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].nextSibling.innerHTML;
        }     
    }
}

The corresponding modification in PHP should be the replacement of the line

print($tx_desc);

by

print('<label for="id_rb_peri">' . $tx_desc . '</label>');
    
02.09.2014 / 19:55
-2

If you prefer to use JavaScript you can do this: $ ("# id_rb_peri"). val (); there you will get the value of it and you can do what you want!

    
02.09.2014 / 20:51