Salman Salman - 1 month ago 4
Javascript Question

Alert is not get called in the onSubmit method

Here is my html code, and i expect when i submit, an alart message appears.

<input id="reportForm" type="submit" value="Submit Report" class="btn btn-primary" />


and here is the js code:

$(document).ready(function () {
$('#reportForm').submit(function () {
alert("Thanks for your report.");
});
});


Unfortunately, it does not call the alert!

Answer

The submit() event handler can only be bound to an form element (not to a input element, as you have done).

You have two options:

(1) Bind the action(s) to the form submission, by changing your HTML to something along these lines --

<form id="reportForm">
    <input type="submit" value="Submit Report" class="btn btn-primary" />
</form>

Or; (2) Bind the action(s) to the button click, by changing your javascript, like so --

$('#reportForm').click(function(){
   alert("Thanks for your report.");
}); 
Comments