Assign a value to checkbox in javascript

Asked

Viewed 1,257 times

1

I have a form that contains some checkbox that when they were marked I wanted to be assigned the value 1 to do the Insert on the server, I’ve seen how to check if it is marked, using Document.getElementByInput, but I assign the tag with the value Vaccine the value of 1 for example ?

Follows the code:

  <form id="Form" method="post" action="php/novoFilhote.php">
        <fieldset id="fieldset2">
        <input id="raca" placeholder="Raça" type="text" />
        <br />
        <input id="mae" placeholder="Mãe" type="text" />
        <br />
        <input id="pai" placeholder="Pai" type="text" />
        <br />
        <input id="preco" placeholder="Preço" type="number" />
        </fieldset>
        <br />
        <fieldset  id="fieldset2">
        <label>Nascimento    </label><input id="nascimento" placeholder="Nascimento" type="date" />
        </fieldset>
        <br />
        <fieldset  id="fieldset2">
        Doente: <input id="doente" name="item" type="checkbox"/>
        </fieldset>
        <br />
        <fieldset  id="fieldset2">
        Vacinado: <input id="vacina" name="item" type="checkbox"/>
        </fieldset>
        <br />
        <fieldset  id="fieldset2">
        Vermifugado: <input id="vermifugado" name="item" type="checkbox"/>
        </fieldset  id="fieldset2">
        <br />
        <fieldset  id="fieldset2">
        Vendido: <input id="vendido" name="item" type="checkbox"/>
        </fieldset  id="fieldset2">
        <br />
        <fieldset  id="fieldset2">
        <input name="MAX_FILE_SIZE" value="102400" type="hidden">
        <input id="image" accept="image/jpeg" type="file">
        </fieldset>
        <br />
        <button class='button2' type="submit">Enviar</button>
    </form>
    </fieldset>
</div>
    </div>
</div>

<script>
function verificaChecks() {
    var aChk = document.getElementsByInput("checkbox"); 
    for (var i=0;i<aChk.length;i++){ 

        if (aChk[i].checked == true){ 
            //Aqui eu teria que atribuir '1'
            alert(aChk[i].value + " marcado.");
        }
    }
}

</script>

2 answers

1

The default behavior of the checkbox is boolean, that is, it will only be sent if it is checked. In your case you are missing only add the values in the checkboxes repectives with the tag value with the value you prefer to receive on the server. Follow in example:

<input id="vacina" name="item" type="checkbox" value="1"/>

How do you have more than one inputwith the same name, the last marked with the same name will overwrite the previous ones, so you should assign a unique name each input to prevent this. For example:

<input id="vacina" name="item[vacina]" type="checkbox" value="1"/>
<input id="vermifugado" name="item[vermifugado]" type="checkbox" value="1"/>

0

Hello below are some notes to help you in programming.

1) it is not correct to assign the same id name to several "fieldset2 objects"

2) it is not correct to assign id to the closing tag </fieldset id="fieldset2">

3) Unknown getElementByInput, use getElementsByName

4) put at the end of all tags’s checkbox the call to the function checkChecks() as shown below

<input id="vermifugado" name="item" type="checkbox" onclick="verificaChecks()"/>

<script>
var aChk = document.getElementsByName('item');
function verificaChecks() {

    for (var i=0;i<aChk.length;i++){ 

        if (aChk[i].checked){ 
            //Aqui eu teria que atribuir '1'
            aChk[i].value = 1;
            alert(aChk[i].id + ' = ' + aChk[i].value + " marcado.");
        }
    }
}
</script>

Browser other questions tagged

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