setar background color do radio button via javascript

Asked

Viewed 141 times

0

I’m learning php and tried something I do on Asp.net but it’s not rolling. I have a page that gets values from a previous page via _GET. In it I have radio type input that I want, that when clicking on a Ubmit, the selected radio turns red. the problem is that by clicking on Ubmit, the previously received value is lost, so the page does not load. How to do this without losing the received value?

the structure of the code is:

html head body form

Php code:

?php

define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__ . '\0 -        Apresentação\Controller\PerguntaController.php');
require_once(__ROOT__ . '\0 -Apresentação\Controller\RespostaController.php');

$Disciplina=$_GET["pergunta"];
$Perguntacontroller = new PerguntaController();
$retorno = $Perguntacontroller->Carregar($Disciplina);

$Respostacontroller = new RespostaController();

$x = 0;
while ($linha1 = mysqli_fetch_array($retorno)) {
    $y=0;
    $Pergunta = $linha1["pergunta"];
    $Id[$x] = $linha1["Id"];
    $certa[$x]=$linha1["certa"];
    $retornoR = $Respostacontroller->Carregar($Id[$x]);

    echo "$Pergunta<br /><br />";

   while ($linha2 = mysqli_fetch_array($retornoR)) {
        $Resposta = $linha2["Resposta"];
        echo"<input type=\"radio\" name=$x value=$y >$Resposta <br /><br />";
        echo"<input type=\"hidden\" name=$x value=$certa[$x]>";
        $y++;
    }
    $x++;
}
?>

Java script code:

<script type="text/javascript">
document.getElementById("submit").onclick = function() {
    var radios = document.getElementsByName("0");
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked) {
            $('input:radio').css('background','#000');
            console.log("Escolheu: " + radios[i].value);
        }
    }
};
</script>
<input id="submit" type="submit">

1 answer

0


Analyzing the code posted, I make three considerations:

1) If you don’t have tag form, you can click on Ubmit the will that goes nowhere.

2) There is no element with the Submit id on your page, so your javascript will never run.

3) When a form is sent with Submit, only form element names and their values are sent, css formatting is not sent.

Responding to the title "setar background color via javascript", follows a code snippet:

<input type="button" value="Me clique" onclick="this.style.backgroundColor='#ff0000'">

  • Opa. 1. I didn’t put the html code so it wouldn’t be long. It has a form. 2. The button with id Submit ended up in the html I didn’t post. 3. About the input code Testarei. Thank you!

  • I took the form and everything worked out. Thanks!

Browser other questions tagged

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