Disable Event-based Inputs Radiobutton Click

Asked

Viewed 63 times

-1

Good morning, everyone! I’m developing a system for creating evidence, and I’m creating a screen to edit questions that are already saved in the database. When selecting the question there are two radio Uttons that search in the database the type of the question (Dissertativa or Objetiva) according to the type already registered in the question creation screen. In other words, it looks for the value in the bank and marks the radio button according to the type of the question. If the question is Objective the system enables the enunciated field and the alternatives from A to E. And if it is Dissertative it disables the alternatives and enables the enunciation. What is happening is that if the question is Dissertativa the system searches the data and disables the alternatives and if I try to change the radio button to Objective it does not enable the fields more of the alternatives. Follows structure of code:

<form method="POST"><br>
        <span class="login100-form-title p-70">Editar Questão Cadastrada</span><br>
            <center>

                <div class="form-check">
                    <input class="form-check-input" type="radio" name="tipo" id="rbDissertativa" value="D" required <?php echo ($tipo == 'D') ? 'checked' : ''; ?> onclick="disableAlternativas()"> 
                    <label class="form-check-label" for="dissertativa">Dissertativa</label>
               </div>

               <div class="form-check">
                   <input class="form-check-input" type="radio" name="tipo" id="rbObjetiva" value="O" required <?php echo ($tipo == 'O') ? 'checked' : ''; ?> onclick="enableAlternativas()">  
                   <label class="form-check-label" for="objetiva" >Objetiva</label>
               </div>

                <label for="enunciado">Enunciado: </label>
                <input type="text" id="enunciado" class="col-sm-6 col-form-label" name="enunciado" size="500" maxlength="500" placeholder="Enunciado" value="<?php echo $enunciado; ?>" <?php echo $podeAlterar; ?>><br><br>
       
                <label for="a">A) </label>
                <input type="text" id="resposta_A" class="col-sm-6 col-form-label" name="resposta_A" size="200" maxlength="200" placeholder="Alternativa A" value="<?php echo $resposta_A; ?>" <?php echo $podeAlterar; ?> disabled><br><br>

                <label for="b">B) </label>
                <input type="text" id="resposta_B" class="col-sm-6 col-form-label" name="resposta_B" size="200" maxlength="200" placeholder="Alternativa B" value="<?php echo $resposta_B; ?>" <?php echo $podeAlterar; ?> disabled><br><br>

                <label for="c">C) </label>
                <input type="text" id="resposta_C" class="col-sm-6 col-form-label" name="resposta_C" size="200" maxlength="200" placeholder="Alternativa C" value="<?php echo $resposta_C; ?>" <?php echo $podeAlterar; ?> disabled><br><br>

                <label for="d">D) </label>
                <input type="text" id="resposta_D" class="col-sm-6 col-form-label" name="resposta_D" size="200" maxlength="200" placeholder="Alternativa D" value="<?php echo $resposta_D; ?>" <?php echo $podeAlterar; ?> disabled><br><br>

                <label for="e">E) </label>
                <input type="text" id="resposta_E" class="col-sm-6 col-form-label" name="resposta_E" size="200" maxlength="200" placeholder="Alternativa E" value="<?php echo $resposta_E; ?>" <?php echo $podeAlterar; ?> disabled><br><br>

                <div>
                    <input type="hidden" name="idquestao" value="<?php echo $idquestao; ?>">
                    <a href="questao.php" class="btn btn-dark">Cancelar</a>
                    <input type="submit" tabindex="1" class="btn btn-dark" name="acao" value="<?php echo $descr_acao; ?>" <?php echo $podeAlterar; ?> >
                </div>
            </center>
    </form>

Function code to disable and enable alternatives:

<script>
    function disableAlternativas() {
        document.getElementById('enunciado').disabled=false
        document.getElementById('resposta_A').disabled=true
        document.getElementById('resposta_B').disabled=true
        document.getElementById('resposta_C').disabled=true
        document.getElementById('resposta_D').disabled=true
        document.getElementById('resposta_E').disabled=true 
    }

    function enableAlternativas() {
        document.getElementById('enunciado').disabled=false
        document.getElementById('resposta_A').disabled=false
        document.getElementById('resposta_B').disabled=false
        document.getElementById('resposta_C').disabled=false
        document.getElementById('resposta_E').disabled=false
        document.getElementById('resposta_D').disabled=false
    }
</script>

Full page code is available here. If anyone can give me a strength <3

  • There is no way to simulate this error, as far as you have posted, the code is not wrong. But what about the rest of your HTML? These elements with id enunciado, resposta_A, resposta_B... are there? There is no duplication of ids on your page? Your browser console shows some error?

  • Good morning. When I change radio button the system should switch between which fields will be enabled or not, it shows no error in the console, I believe that only is not executing the function that disables and enables the fields of the screen. Follows full code:

  • https://github.com/MAUPEARS/editar_prova/blob/master/page_1

1 answer

0

Based on code you released on Github, I removed the PHP snippets because they are not replicable for the example, the html snippet for the Form would be as follows:

    <span class="login100-form-title p-70">Editar Questão Cadastrada</span><br>
        <center>

            <div class="form-check">
                <input class="form-check-input" type="radio" name="tipo" id="rbDissertativa" value="D" required onClick="disableAlternativas()"> 
                <label class="form-check-label" for="dissertativa">Dissertativa</label>
           </div>

           <div class="form-check">
               <input class="form-check-input" type="radio" name="tipo" id="rbObjetiva" value="O" required onClick="enableAlternativas()" >  
               <label class="form-check-label" for="objetiva" >Objetiva</label>
           </div>

            <label for="enunciado">Enunciado: </label>
            <input type="text" id="enunciado" class="col-sm-6 col-form-label" name="enunciado" size="500" maxlength="500" placeholder="Enunciado" ><br><br>
   
            <label for="a">A) </label>
            <input type="text" id="resposta_A" class="col-sm-6 col-form-label" name="resposta_A" size="200" maxlength="200" placeholder="Alternativa A"><br><br>

            <label for="b">B) </label>
            <input type="text" id="resposta_B" class="col-sm-6 col-form-label" name="resposta_B" size="200" maxlength="200" placeholder="Alternativa B" /> <br><br>

            <label for="c">C) </label>
            <input type="text" id="resposta_C" class="col-sm-6 col-form-label" name="resposta_C" size="200" maxlength="200" placeholder="Alternativa C" ><br><br>

            <label for="d">D) </label>
            <input type="text" id="resposta_D" class="col-sm-6 col-form-label" name="resposta_D" size="200" maxlength="200" placeholder="Alternativa D"><br><br>

            <label for="e">E) </label>
            <input type="text" id="resposta_E" class="col-sm-6 col-form-label" name="resposta_E" size="200" maxlength="200" placeholder="Alternativa E"> <br>

            <div>
                <input type="hidden" name="idquestao" >
                <a href="questao.php" class="btn btn-dark">Cancelar</a>
                <input type="submit" tabindex="1" class="btn btn-dark" name="acao" >
            </div>
        </center>

Already the script would be:

        function disableAlternativas() {
            document.getElementById('enunciado').disabled=false;
            document.getElementById('resposta_A').disabled=true;
            document.getElementById('resposta_B').disabled=true;
            document.getElementById('resposta_C').disabled=true;
            document.getElementById('resposta_D').disabled=true;
            document.getElementById('resposta_E').disabled=true ;
        }

        function enableAlternativas() {
            document.getElementById('enunciado').disabled=false
            document.getElementById('resposta_A').disabled=false
            document.getElementById('resposta_B').disabled=false
            document.getElementById('resposta_C').disabled=false
            document.getElementById('resposta_E').disabled=false
            document.getElementById('resposta_D').disabled=false
        }

You can test code functionality in Jsfiddle.

Browser other questions tagged

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