mercredi 16 septembre 2020

Ignore unchecked checkbox in GET form

I am using a piece of code to ignore empty input boxes when processing a form with the GET method. It is working for the text inputs but not for the checkboxes I have in the form. The code I am using is

function SubmitForm(pForm) {
var getString = "";
var elems = pForm.getElementsByTagName('input');
for(var i = 0; i < elems.length; i++) {
    if(elems[i].type == "submit") {
        continue;
    }
    if(elems[i].value != "") {
        getString += encodeURIComponent(elems[i].name) + "=" + encodeURIComponent(elems[i].value) + "&";
    }
    }
    window.location = pForm.action + getString;
return false;
    }

The HTML for my form is

<form action="game_finder.php?" method="GET" onsubmit="return SubmitForm(this);">
<input type="checkbox" name="system[]" value="Warhammer 40k">Warhammer 40k&nbsp;&nbsp;
<input type="checkbox" name="system[]" value="Kill Team">Kill Team&nbsp;&nbsp;
<input type="checkbox" name="system[]" value="Warhammer Quest: Blackstone Fortress">Warhammer Quest: 
 Blackstone Fortress&nbsp;&nbsp;
<input type="checkbox" name="system[]" value="Necromunda">Necromunda&nbsp;&nbsp;

Any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire