jquery Event Preventdefault () method Usage instance

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.