Jquery easyui異步提交表單的兩種方式示例詳解
這篇文章分享一下easyui常用的兩種表單異步提交的方式。
開始前的準(zhǔn)備工作
1、使用HBuilderX創(chuàng)建一個(gè)簡單的html項(xiàng)目,刪除img和html目錄,只保留js目錄和index.html;
2、下載jquery.min.js和jquery.easyui.min.js,復(fù)制到j(luò)s目錄下;
3、修改index.html的代碼,增加一個(gè)表單,包含三個(gè)輸入框和一個(gè)提交按鈕,在頁面引入easyui的js文件;
頁面效果如下:
方式一:利用jquery ajax提交
這種方式只需要引入jquery.min.js
$.post()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>easyui異步提交表單</title> <script src="js/jquery.min.js"></script> </head> <body> <form id="ajax_form"> 姓名:<input id="name" /> 年齡:<input id="age" /> 手機(jī)號:<input id="phone" /> <button type="button" id="submit">提交</button> </form> <script> $(function( $("#submit").click(function() { $.post("/xxx/save", { name: $("#name").val(), age: $("#age").val(), phone: $("#phone").val() }, function(resp) { // 處理響應(yīng)的數(shù)據(jù) }, "json"); }); )); </script> </body> </html>
$.ajax()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>easyui異步提交表單</title> <script src="js/jquery.min.js"></script> </head> <body> <form id="ajax_form"> 姓名:<input id="name" /> 年齡:<input id="age" /> 手機(jī)號:<input id="phone" /> <button type="button" id="submit">提交</button> </form> <script> $(function( $("#submit").on("click", function() { $.ajax({ url: "/xxx/save", type: "post", data: { name: $("#name").val(), age: $("#age").val(), phone: $("#phone").val() }, async: true, cache: false, dataType: "json", processData: true, success: function(resp) { // 處理成功的響應(yīng) }, error: function(resp) { // 處理失敗的響應(yīng) } }); }); )); </script> </body> </html>
方式二:使用easyui提供的表單提交方式
easyui官網(wǎng)已經(jīng)介紹了這種方式,不過和上面的ajax提交不一樣,這種提交方式要給輸入框設(shè)置name屬性。
案例代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>easyui異步提交表單</title> <script src="js/jquery.min.js"></script> <script src="js/jquery.easyui.min.js"></script> </head> <body> <form id="ajax_form" method="post"> 姓名:<input name="name" /> 年齡:<input name="age" /> 手機(jī)號:<input name="phone" /> <button type="submit">提交</button> </form> <script> let requestUrl = "/xxx/save"; $(document).ready(function() { $("#ajax_form").form({ url: requestUrl, success: function(resp) { // 處理成功的響應(yīng) } }); }); </script> </body> </html>
到此這篇關(guān)于Jquery easyui異步提交表單的兩種方式的文章就介紹到這了,更多相關(guān)jquery easyui異步提交表單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js頁面滾動(dòng)時(shí)層智能浮動(dòng)定位實(shí)現(xiàn)(jQuery/MooTools)
關(guān)于層的智能浮動(dòng)效果早在幾年前我就在國外的一些個(gè)人網(wǎng)站的垂直導(dǎo)航上見到了,現(xiàn)在似乎在國內(nèi)一些商業(yè)網(wǎng)站上也屢見此效果2011-08-08JavaScript 彈出子窗體并返回結(jié)果到父窗體的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄狫avaScript 彈出子窗體并返回結(jié)果到父窗體的實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05easyui combotree加載靜態(tài)數(shù)據(jù)問題(選不上)解決方法
這篇文章主要介紹了easyui combotree加載靜態(tài)數(shù)據(jù)問題,選不上)2016-12-12jQuery中$this和$(this)的區(qū)別介紹(一看就懂)
這篇文章主要介紹了jQuery中$this和$(this)的區(qū)別介紹(一看就懂),本文用簡潔的語言講解了它們之間的區(qū)別,并給出了一個(gè)例子來說明,需要的朋友可以參考下2015-07-07jQuery插件HighCharts繪制2D餅圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制2D餅圖效果,結(jié)合完整實(shí)例形式分析了jQuery使用HighCharts插件繪制餅圖效果的操作步驟與相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03用JQuery實(shí)現(xiàn)全選與取消的兩種簡單方法
本篇文章主要是對JQuery實(shí)現(xiàn)全選與取消的兩種簡單方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02分享有關(guān)jQuery中animate、slide、fade等動(dòng)畫的連續(xù)觸發(fā)、滯后反復(fù)執(zhí)行的bug
這篇文章主要介紹了分享有關(guān)jQuery中animate、slide、fade等動(dòng)畫的連續(xù)觸發(fā)、滯后反復(fù)執(zhí)行的bug的相關(guān)資料,需要的朋友可以參考下2016-01-01Javascript中的異步編程規(guī)范Promises/A詳細(xì)介紹
這篇文章主要介紹了Javascript中的異步編程規(guī)范Promises/A詳細(xì)介紹,同時(shí)介紹了jQuery 中的 Deferred 和 Promises,需要的朋友可以參考下2014-06-06