How to trigger Jquery event on form action after page loaded

Viewed 46

I have a checkbox which is preselected checkbox and after clicking it. jQuery function is called and display the form action.

<form id="lcform" action="${requestContextPath }/checkout/applyLPDisc.mobi" method="get">
    <input type="checkbox" id="lch" name="lch" value="Loyal" checked>Loyalty Discount<br>
</form>

I have requirement to load form action on the basis of pre-selected checkbox and the jQuery event is triggered once the page loads i.e. no need to click the checkbox.

I have requirement to load form action on the basis of pre-selected checkbox and the Jquery event is triggered once the page loads i.e. no need to click the checkbox. jQuery function :

$('#lch').bind('click', function() {
    if(this.checked == true){
        document.getElementById('lcform').submit();
    }
});

I have requirement to load form action on the basis of pre-selected checkbox and the Jquery event is triggered once the page loads i.e. no need to click the checkbox.

2 Answers

You can try this for example:

$(document).ready(function() {
    if($('#lch').is(":checked") == true){
        document.getElementById('lcform').submit();
    }
});

Tell me if it works!

Can be done simply as follows,

$(document).ready(function() {

  ($('#lch').is(":checked"))? document.getElementById('lcform').submit() : false;   

});
Related