There is a form on the bootstrap with the submit button, so I need to override the request that is sent by clicking on this button. But that all logic of operation of the submit button remained. That is, a check for required fields. How can this be implemented?
2 answers
<form name="contact_form" method="post" action="dumb.htm" onsubmit="return validate_form ( );"> <h1>Пожалуйста введите Ваше имя.</h1> <p>Ваше имя: <input type="text" name="contact_name"></p> <input type="submit" name="send" value="Отправить данные"></p> </form> <script type="text/javascript"> function validate_form ( ) { valid = true; if ( document.contact_form.contact_name.value == "" ) { alert ( "Пожалуйста заполните поле 'Ваше имя'." ); valid = false; } return valid; } </script> Also, the form tag contains an onsubmit attribute to invoke the JavaScript validate function validate () when the "Send data" button is clicked.
|
In fact, you can hang any handler on the submit button or on the form itself, the main thing is NOT inserts the following line
e.preventDefault();
where e is the event parameter when handling the submit event. This line cancels the standard action on the submit event (usually a form submission) and perform custom actions. Validation of fields usually does just that (jQuery 'form.validate').
Those.
jQuery("#form").submit(function(e) { e.preventDefault(); // это отменяет! действие базового submit // do something return false; //чтобы форма не отправилась, и true для отправки }); There can be any number of handlers on submit, just the sequence of their call will be in the order in which they are declared.
|