使用ajax提交表單

來源:互聯網
上載者:User

標籤:style   blog   color   os   使用   io   java   ar   for   

1、ajaxForm

作用:

(1)預先載入js事件到form

(2)提交action前js驗證表單

(3)提交action後,結果由js處理,不自動跳轉

要求:

<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script><script type="text/javascript" src="js/jquery.form.js"></script>

樣本:

<script type="text/javascript">            //var error = document.getElementById(‘errorDiv‘);            //注意的是var error只在這裡聲明,不要賦值 ,賦值也是null            var error;            // wait for the DOM to be loaded             $(document).ready(function() {                error = document.getElementById(‘errorDiv‘);                // bind ‘myForm‘ and provide a simple callback function                   var options = {                    //target:        ‘#output1‘,   // target element(s) to be updated with server response                     beforeSubmit : validate,// pre-submit callback                     dataType : ‘json‘,// ‘xml‘, ‘script‘, or ‘json‘ (expected server response type)                     //dataType必須放在callback(json)前面,因為callback(json)要調用json這個變數                    success : function(json) {                        json = eval(json);                        if (json.errcode == 0) {                            window.location.href = ‘seller/seller.jsp‘;                        } else {                            error.innerHTML = "帳號和密碼錯誤,請重新登入.";                        }                    }                // post-submit callback                 // other available options:                 //url:       url         // override for form‘s ‘action‘ attribute                 //type:      type        // ‘get‘ or ‘post‘, override for form‘s ‘method‘ attribute                 //clearForm: true        // clear all form fields after successful submit                 //resetForm: true        // reset the form after successful submit                 // $.ajax options can be used here too, for example:                 //timeout:   3000                 };                $(‘#loginForm‘).ajaxForm(options);            });            function validate() {                var username = document.getElementById("name").value;                var password = document.getElementById("pwd").value;                //檢測是否為空白可以將其交給前台處理;                  if (username.length == 0) {                    error.innerHTML = "帳號不可為空";                    return false;                } else if (password.length == 0) {                    error.innerHTML = "密碼不可為空";                    return false;                }                error.innerHTML = "正在登入";            }        </script>

 

 

Done

使用ajax提交表單

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.