Skip to content Skip to sidebar Skip to footer

How To Let A Submit Button In A Form Do Something Before Executing The Action Of The Form

Good evening, i have a HTML form that contains a submit button that does something other than executing the action of the form, i want these submit button to do an action first the

Solution 1:

$(".btn1").on('click', function(e) {
    e.preventDefault()

    var self = this;
    $('#toggleText').slideUp('fast', function() {
         self.form.submit();
    });
});

Solution 2:

You should put the slide up in a method and call it on submit

<form action="AddNewCar" onsubmit="slideupmethod" method="get">

function slideupmethod(){
   $('#toggleText').slideUp();
}

Solution 3:

You can create a submit button, which uses onclick to fire off a function, and then submit your form. For example.

The html button for submit

<input type="button" value="Submit Order" class="btn_submit" onclick="submitOrder()" />

your javascript function

//function called when submit button pressed
function submitOrder() {

    //perform client side form validation and other things

    //submit the form
    $('#yourFormId').submit();
}

Alternatively, using the button you currently have. You can prevent the form from submitting when the button is pressed, perform the required actions, and then submit the form.

javascript

$('form').submit(function(evt){
//prevent form from submitting
evt.preventDefault();
//do required actions

//submit form
$('form').submit();

});

Post a Comment for "How To Let A Submit Button In A Form Do Something Before Executing The Action Of The Form"