简体   繁体   中英

If checkbox checked then trick button to continue to next page

How would I do this is Javascript? I'm trying to figure out a way in Javascript to trigger that submit button below if checkbox is checked. Which is it.

<input type="checkbox" name="product[6]" value="1" checked="checked">

<input type="submit" value="Continue">

Thank for you any help!

Tim

For simplicity and specificity of which checkbox am adding ID's to elements

<input id="product6" type="checkbox" name="product[6]" value="1" checked="checked">    
<input id="submit-btn" type="submit" value="Continue">

jQuery:

$(function(){

    if( $('#product6').is(':checked') ){
        $('#submit-btn').click();
        // or submit the form
        $('#product6').closest('form').submit();
    }

});

UPDATED

You can handle submit() event and add condition like bellow :

JS :

$("#target").submit(function( event ) {
    event.preventDefault(); //prevent submit action here

    //Condition on checkbox
    if($( "input[name='product[6]']:checked" )){
        $('#submit-btn').click(); //handle submit if condition true
    }
});

First you want to give the checkbox input an id (checkbox-id for example) and for the submit input (submit-id for example)

document.getElementById("checkbox-id").onclick = myFunction;

function myFunction()
{
    document.getElementById("submit-id").click();
    //Or
    document.getElementById("submit-id").submit();
}

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