Assigning a value to the checkbox in javascript

1

I have a form that contains some checkbox that when they are marked I wanted to be assigned the value 1 to do the Insert on the server, I have already 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?

Follow 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>
    
asked by anonymous 06.06.2016 / 02:55

2 answers

1

The default behavior of the checkbox is Boolean, that is, its value will only be sent if it is checked. In your case, you just need to add the values in the respective checkboxes with the value tag to the value you prefer to receive on the server. Here is an example:

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

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

<input id="vacina" name="item[vacina]" type="checkbox" value="1"/>
<input id="vermifugado" name="item[vermifugado]" type="checkbox" value="1"/>
    
06.06.2016 / 03:52
0

Hello, here are some notes to help you with programming.

1) It is not correct to assign the same id name to multiple "fieldset2" objects

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

3) I do not know getElementByInput, use getElementsByName

4) put at the end of all the check's tag the call to the check function Checks () 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>
    
06.06.2016 / 04:15