简体   繁体   中英

javascript function to uncheck input checkbox before sending

I have difficulty to solve this and would ask your help ! I'm trying to make a javascript but i had no success i have inside a form below, two input checkbox, when the user press the submit i want to verify if the two checkbox is checked, if they are checked i want to disable the two before sending it to another page, and if only one of then is checked, i want to do nothing.

<form action="{$GLOBALS.site_url}/search/">
<input type="checkbox" checked = "checked"  name="new[equal]" value="1" /> New <br>
<input type="checkbox" checked = "checked"  name="used[equal]" value="1" /> Used <br>
<input type="submit" class="button" value="[[Find:raw]]" />
</form>

thank you friends

Have a look here : http://api.jquery.com/checked-selector/

It explains to you how you can use jquery to check if a checkbox is checked or not.

You can try this.

var new = document.forms[0]["new[equal]"],
    used = document.forms[0]["used[equal]"]
    if(new.checked && used.checked){
        new.disabled = true;
        used.disabled = true;
    }

If you have multiple forms on the page then you should provide a name to the form and use the form name to select the required form.

Something like this will do.

document.formName.elementName or document.formName['elementName']

Update: If you want to validate this on submit button click then you can create a JS function with above code and call it on submit button click

HTML

<input type="submit" onclick="ValidateForm()" value="Submit" />

JS

function ValidateForm(){
   var new = document.forms[0]["new[equal]"],
        used = document.forms[0]["used[equal]"]
        if(new.checked && used.checked){
            new.disabled = true;
            used.disabled = true;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM