This article mainly introduced the jquery event Preventdefault () method usage, the example analyzes the Preventdefault () method The function and the use skill, needs the friend to be possible to refer to under
-->
This example describes the jquery event Preventdefault () method usage. Share to everyone for your reference. The implementation methods are as follows:
The code is as follows: <title>jquery event: Use of Preventdefault () method </title>
<script type= "Text/javascript" src= "Jquery-1.8.2.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#myform"). Submit (function (e) {
if ($ ("#search"). val () = "") {
Alert ("Search content cannot be empty");
E.preventdefault ();
}
});
});
</script>
<body>
<form action= "http://www.baidu.com" method= "post" id= "MyForm" >
<input type= "text" size= "id=" search/><input type= "Submit" value= "Searching"/>
</form>
</body>
Supplementary Note:
(1) If you want to get information about the event, add an argument to the anonymous function. An example of E represents an object of an event.
(2) by invoking the Preventdefault () method of the event object, it is possible to prevent the default behavior from occurring (as in the case of sending to http://www.baidu.com).
In addition, invoking the Stoppropagation () method of the event object can prevent bubbling events from occurring. About the bubble model and capture model, we can refer to another article "JS in the event capture model and bubble model case analysis."
I hope this article will help you with your jquery programming.