Home >Web Front-end >JS Tutorial >How Can I Prevent Form Submission Without Changing the Submit Button?
In a scenario where a form contains a submit button that cannot be directly modified, disabling the form submission becomes a necessity. To achieve this, consider the following:
Returning false from the event handler, such as onsubmit, intercepts the submit event and prevents the form from being submitted. However, it has limitations as JavaScript errors occurring before the return statement result in an automatic form submission.
To address this, combine return false with preventDefault() within the event handler. preventDefault() inhibits the default form action, even in the presence of JavaScript errors, ensuring the form remains unsubmitted.
Alternatively, employ a try...catch block within the event handler to isolate any JavaScript exceptions. The catch block can throw an error, further preventing the form submission. This method provides better error handling compared to the former approach.
The above is the detailed content of How Can I Prevent Form Submission Without Changing the Submit Button?. For more information, please follow other related articles on the PHP Chinese website!