Jquery ajax 簡潔方式提交表單form

來源:互聯網
上載者:User

最簡單就這樣

 

 代碼如下 複製代碼
$.post("/index.php?action=ajax&rs=TWProAjax::checkProTermsExisted", $(this).serialize(), function(data){
},'json');

注意serialize就是把你表單所有input以post形式或get形式提交過去了,下面看個執行個體


我們有一個非常普通的表單:

首先,建立Login.html頁面:

 代碼如下 複製代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>$.ajax()方法發送請求</title>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
    <style type="text/css">
        body
        {
            font-size: 13px;
        }
        .divFrame
        {
            width: 225px;
            border: solid 1px #666;
        }
        .divFrame .divTitle
        {
            padding: 5px;
            background-color: #eee;
            height: 23px;
        }
        .divFrame .divTitle span
        {
            float: left;
            padding: 2px;
            padding-top: 5px;
        }
        .divFrame .divContent
        {
            padding: 8px;
            text-align: center;
        }
        .divFrame .divContent .clsShow
        {
            font-size: 14px;
            line-height: 2.0em;
        }
        .divFrame .divContent .clsShow .clsError
        {
            font-size: 13px;
            border: solid 1px #cc3300;
            padding: 2px;
            display: none;
            margin-bottom: 5px;
            background-color: #ffe0a3;
        }
        .txt
        {
            border: #666 1px solid;
            padding: 2px;
            width: 150px;
            margin-right: 3px;
        }
        .btn
        {
            border: #666 1px solid;
            padding: 2px;
            width: 50px;
        }
    </style>
    <script type="text/javascript">
        $(function () {
            $("#txtName").focus();//輸入焦點
            $("#txtName").keydown(function (event) {
                if (event.which == "13") {//斷行符號鍵,移動游標到密碼框
                    $("#txtPass").focus();
                }
            });
            $("#txtPass").keydown(function (event) {
                if (event.which == "13") {//斷行符號鍵,用.ajax提交表單
                    $("#btnLogin").trigger("click");
                }
            });
            $("#btnLogin").click(function () { //“登入”按鈕單擊事件
                //擷取使用者名稱稱
                var strTxtName = encodeURI($("#txtName").val());
                //擷取輸入密碼
                var strTxtPass = encodeURI($("#txtPass").val());
                //開始發送資料
                $.ajax
                ({ //請求登入處理頁
                    url: "Login.aspx", //登入處理頁
                    dataType: "html",
                    //傳送請求資料
                    data: { txtName: strTxtName, txtPass: strTxtPass },
                    success: function (strValue) { //登入成功後返回的資料
                        //根據傳回值進行狀態顯示
                        if (strValue == "True") {//注意是True,不是true
                            $(".clsShow").html("操作提示,登入成功!" + strValue);
                        }
                        else {
                            $("#divError").show().html("使用者名稱或密碼錯誤!" + strValue);
                        }
                    }
                })
            })
        })
    </script>
</head>
<body>
    <form id="frmUserLogin">
    <div class="divFrame">
        <div class="divTitle">
            <span>使用者登入</span>
        </div>
        <div class="divContent">
            <div class="clsShow">
                <div id="divError" class="clsError">
                </div>
                <div>
                    名稱:<input id="txtName" type="text" class="txt" /></div>
                <div>
                    密碼:<input id="txtPass" type="password" class="txt" /></div>
                <div>
                    <input id="btnLogin" type="button" value="登入" class="btn" />&nbsp;&nbsp
                    <input id="btnReset" type="reset" value="取消" class="btn" />
                </div>
            </div>
        </div>
    </div>
    </form>
</body>
</html>

然後,建立Login.aspx,接收並處理資料

 代碼如下 複製代碼

:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="JSDemo.Login" ResponseEncoding="gb2312"%>

 
<%
    string strName = System.Web.HttpUtility.UrlDecode(Request["txtName"]);
    string strPass = System.Web.HttpUtility.UrlDecode(Request["txtPass"]);
    bool login = false;
    if (strName == "admin" && strPass == "admin")
    {
        login = true;
    }
    Response.Write(login);
%>


最關鍵就是下面這段代碼

 代碼如下 複製代碼

   $("#btnLogin").click(function () { //“登入”按鈕單擊事件
                //擷取使用者名稱稱
                var strTxtName = encodeURI($("#txtName").val());
                //擷取輸入密碼
                var strTxtPass = encodeURI($("#txtPass").val());
                //開始發送資料
                $.ajax
                ({ //請求登入處理頁
                    url: "Login.aspx", //登入處理頁
                    dataType: "html",
                    //傳送請求資料
                    data: { txtName: strTxtName, txtPass: strTxtPass },
                    success: function (strValue) { //登入成功後返回的資料
                        //根據傳回值進行狀態顯示
                        if (strValue == "True") {//注意是True,不是true
                            $(".clsShow").html("操作提示,登入成功!" + strValue);
                        }
                        else {
                            $("#divError").show().html("使用者名稱或密碼錯誤!" + strValue);
                        }
                    }
                })

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.