亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Ajax方法實(shí)現(xiàn)Form表單的提交及注意事項(xiàng)

 更新時(shí)間:2017年07月18日 10:47:16   作者:涵  
這篇文章主要介紹了使用Ajax方法實(shí)現(xiàn)Form表單的提交及注意事項(xiàng),需要的朋友可以參考下

寫在前面的話

在使用form表單的時(shí)候,一旦點(diǎn)擊提交觸發(fā)submit事件,一般會(huì)使得頁(yè)面跳轉(zhuǎn),頁(yè)面間的跳轉(zhuǎn)等行為的控制權(quán)往往在后端,后端會(huì)控制頁(yè)面的跳轉(zhuǎn)及數(shù)據(jù)傳遞,但是在某些時(shí)候不希望頁(yè)面跳轉(zhuǎn),或者說想要將控制權(quán)放在前端,通過js來操作頁(yè)面的跳轉(zhuǎn)或者數(shù)據(jù)變化。

一般這種異步的操作,我們都會(huì)想到ajax方式,因此在實(shí)現(xiàn)了功能后就整理了這篇文章,通過ajax方法實(shí)現(xiàn)form表單的提交并進(jìn)行后續(xù)的異步操作。

常見的form表單提交方式

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>login test</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="login test">  
</head>
<body>
<div id="form-div">
  <form id="form1" action="/users/login" method="post">
    <p>用戶名:<input name="userName" type="text" id="txtUserName" tabindex="1" size="15" value=""/></p>
    <p>密 碼:<input name="password" type="password" id="TextBox2" tabindex="2" size="16" value=""/></p>
    <p><input type="submit" value="登錄">&nbsp<input type="reset" value="重置"></p>
  </form>
</div>
</body>
</html>

點(diǎn)擊登錄按鈕后,即觸發(fā)form表單的提交事件,數(shù)據(jù)傳輸至后端,由后端控制頁(yè)面跳轉(zhuǎn)和數(shù)據(jù)。

ajax實(shí)現(xiàn)form提交方式

修改完成后代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>login test</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="ajax方式">
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script type="text/javascript">
    function login() {
      $.ajax({
      //幾個(gè)參數(shù)需要注意一下
        type: "POST",//方法類型
        dataType: "json",//服務(wù)端接收的數(shù)據(jù)類型
        url: "/users/login" ,//url
        data: $('#form1').serialize(),
        success: function (result) {
          console.log(result);//打印服務(wù)端返回的數(shù)據(jù)(調(diào)試用)
          if (result.resultCode == 200) {
            alert("SUCCESS");
          }
          ;
        },
        error : function() {
          alert("異常!");
        }
      });
    }
  </script>
</head>
<body>
<div id="form-div">
  <form id="form1" onsubmit="return false" action="##" method="post">
    <p>用戶名:<input name="userName" type="text" id="txtUserName" tabindex="1" size="15" value=""/></p>
    <p>密 碼:<input name="password" type="password" id="TextBox2" tabindex="2" size="16" value=""/></p>
    <p><input type="button" value="登錄" onclick="login()">&nbsp;<input type="reset" value="重置"></p>
  </form>
</div>
</body>
</html>

注意事項(xiàng)

  • 在常用方式中,點(diǎn)擊的登錄按鈕的type為"submit"類型;
  • 在常用方式中,form的action不為空;
  • ajax方式中需要注意的是$.ajax方法中的參數(shù):dataType和data。

我平時(shí)很少寫前端代碼,級(jí)別也就是入門級(jí)別,能看懂能改而已,所以很多時(shí)候都是百度,像這次這個(gè)功能的實(shí)現(xiàn)也是借助了百度,但是,我百度到的代碼在$.ajax方法中設(shè)置的dataType參數(shù)值為"html"而不是"json",導(dǎo)致我在一開始調(diào)試的時(shí)候一直報(bào)錯(cuò),最終是改成了"json"才成功,因此在這里特別說明并提醒一下,別和我一樣走錯(cuò)了路,還有就是向服務(wù)端傳輸?shù)膁ata值了,像上面代碼一樣,將form表單中的數(shù)據(jù)序列化傳輸即可。

以上所述是小編給大家介紹的使用Ajax方法實(shí)現(xiàn)Form表單的提交及注意事項(xiàng),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論