How to make all checkboxes unchecked and the value empty inside if?

Asked

Viewed 84 times

1

My need to allow: With the radio selection he can choose or not the checkbox corresponding to the radio ID. And if the user selects a checkbox in addition (>1) I send an Alert informing that there was a second check selection and clear the fields. However I am not able to clear the checkbox after the second click.

function testaCheck(idMaster) {
    var inputs, i, checados = 0;
    inputs = document.getElementsByTagName("input");
    for (i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            if (inputs[i].checked == true) {
                checados++;
                document.getElementsByName('DS_MD' + idMaster).value = idMaster;
                $("#formSelectMAWB").find("#masterDireto").val(idMaster);
            }
            if (checados > 1) {
                alert("Só pode haver um item selecionado.");
                inputs[i].checked = false;
            }
        }
    }
};
<label class="input-control radio">
    <input type="radio" name="idMAWB" id="idMAWB" value="<%=ID_CD_MAWB%>" onClick="selecionaMAWB(this.value)">
    <span class="helper"><%=DS_MAWB%></span>
</label>    

<label for="DS_MD<%=ID_CD_MAWB%>">
    <input type="checkbox" name="DS_MD<%=ID_CD_MAWB%>" id="masterDireto<%=ID_CD_MAWB%>" onClick="testaCheck(<%=ID_CD_MAWB%>)">
    <span class="helper"> <%=ID_CD_MAWB%> </span>
</label>
  • What is the relation of the ASP in the problem?

  • I left ASP because I don’t know if there is a difference between JS that is used with ASP for use with other languages. I am learning about ASP and JS at the stage where I am.

  • Well, it seems to me that your problem is Javascript only, so I see no reason to use the tag [tag:]

  • Got it, so I removed the Asp tag. Thanks for the tip Sam.

  • That function selecionaMAWB() is what? It doesn’t have it in the code.

  • The selecionaMAWB() is to bring the value that corresponds to ID_CD_MAWB is something that already existed in the system since creation. Ex: ID_CD_MAWB - 17 ; MAWB - 417 2541 5571

Show 1 more comment

2 answers

2


You can do unknowing all checkbox with:

$("input:checkbox")     // seleciona todos os checkbox
.prop("checked", false) // desmarca todos
.val('');               // esvazia o value de todos

Since you are using jQuery, take advantage and use it which is easier to do what you want. Just change the line inputs[i].checked = false; by the code above.

Now, there’s a problem in your code, here:

document.getElementsByName('DS_MD' + idMaster).value = idMaster;

Missing put the index [0], if it’s not going to work, because the getElementsByName creates a list of nodes, and does not select an element. So it should look like this:

document.getElementsByName('DS_MD' + idMaster)[0].value = idMaster;

Another problem:

It seems to me that you are repeating this id:

id="idMAWB"

Know that repeating id is wrong in HTML. Do as you did in other id’s, so it doesn’t get repeated:

id="idMAWB<%=ID_CD_MAWB%>"
  • I did the tests with what explained to me but the amount that the check passes to be marked has not been cleaned. With respect to id="idMAWB", I believe I cannot make a change like this because this field is called in another file .asp and seems to be linked to SQL Server.

  • Is this one: $("#formSelectMAWB").find("#masterDireto").val(idMaster)?

  • the id="idMAWB is ID_CD_MAWB within SQL Server, the $("#formSelectMAWB").find("#masterDireto").val(idMaster) is the value to be passed to the bank to inform if the operation is a masterDirect (checked box signals that it is masterDirect and so the Master ID code returns to the database in the column 'DS_MS')

1

Thank you very much for your help Sam, thanks to your reply I was able to leave the code according to the need of the company where I do internship. Follow the final code so you can be of help to someone.

function testaCheck(idMaster) {
    var inputs, i, checados = 0;
    inputs = document.getElementsByTagName("input");
    for (i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            if (inputs[i].checked == true) {
                checados++;
                document.getElementsByName('DS_MD' + idMaster).value = idMaster;
                $("#formSelectMAWB").find("#masterDireto").val(idMaster);
            }

        }

    }
    if (checados > 1) {
                uncheckAll(inputs);
                document.getElementsByName('DS_MD' + idMaster).value = null;
                $("#formSelectMAWB").find("#masterDireto").val(null);
            }
};  

function uncheckAll(inputItems){
    alert("Só pode haver um item selecionado. Favor refazer a seleção");
    for (i = 0; i < inputItems.length; i++) {
        inputItems[i].checked = false;
    }
}

Browser other questions tagged

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