Detailed explanation of how to use the jquery form plug-in form
Traditional form submissions are all in the form of page jumps, but ajax submission is more popular now. So if you want to have the simplicity of form submission and the effect of ajax, is there any solution?
How to use
Two ways to use:
The first way
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery-1.9.1.min.js"></script> <script src="js/jquery-form.js"></script> <script> // 使用ajaxForm $(function(){ $("#myForm").ajaxForm(function(){ $("#output1").html("提交成功").show(); }) }) </script> </head> <body> <form id="myForm"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit" value="提交"> <div id="output1" style="display: none"></div> </form> </body> </html>
Second way
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery-1.9.1.min.js"></script> <script src="js/jquery-form.js"></script> <script> $(function(){ //方式二 与方式一效果一样 $("#myForm").submit(function(){ // 使用ajaxSubmit $(this).ajaxSubmit(function(){ $("#output1").html("提交成功").show(); }); return false; }) }) </script> </head> <body> <form id="myForm"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit" value="提交"> <div id="output1" style="display: none"></div> </form> </body> </html>
It feels like the first way is more convenient.
The parameter function() is the callback function after successful submission.
Using this submission method, asynchronous form submission can be achieved, which is very convenient. However, it is still a bit unsatisfactory. For example, I may want to verify before submitting the form. Although it can be done manually outside of the form submission action, it is very troublesome. Does the form plug-in inherit such functionality?
2. Options parameter
The above only talks about one function callback function parameter in the form. In fact, it has another parameter, which is options. what's the function?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery-1.9.1.min.js"></script> <script src="js/jquery-form.js"></script> <script> var options = { target:'#output1', // 把服务器返回的内容放入id为output1的元素中 beforeSubmit: fun1, // 提交前的回调函数 success: fun2, // 提交后的回调函数 dataType: // 接收服务端返回的类型 xml,scrpit,json }; // beforeSubmit前可以作验证 function fun1(formData,jqForm,options){ // formData 提交值的数组对象 // jqForm 表单元素的jQuery对象,jqForm[0]是其dom对象 // 该函数如果返回false,则阻止表单提交 // formData可以判断全部不为空的情况 for(var i=0;i<formData.length;i++){ if(!formData[i].value){ alert("都不能为空"); return false; } } // jqForm可以判断某个不为空的情况 var form = jqForm[0]; if(!form.name.value){ alert("name不能为空"); return false; } // fieldValue()可以获取多值的数组形式,如checkbox var value = $('input[name=name]').fieldValue(); if(!value[0]){ return false; } } function fun2(responseText, statusText){ // 根据dataType不同responseText解析方式不同, // 默认 responseText // xml responseXml以xml解析 // json responseJson } $(function(){ //方式二 与方式一效果一样 $("#myForm").ajaxForm(options); // 要想使options生效,需要作为参数传递 }) </script> </head> <body> <form id="myForm"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit" value="提交"> <div id="output1" style="display: none"></div> </form> </body> </html>
It can be seen that in the callback function fun1 of beforeSubmit, we have three ways to obtain form data: formData, jqForm, fieldValue, which satisfy various ways of obtaining values, and you can verify how you want. As long as it returns false, it can prevent the form from being submitted. The fun2 of the sucess callback also has status values and return data from the server. You can handle it how you want.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
