当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Jquery.Form 异步提交表单的简单实例

Jquery.Form 异步提交表单的简单实例

2019年03月22日  | 移动技术网IT编程  | 我要评论

jquery.form 异步提交表单的简单实例。

1. 在你的页面里写一个表单。一个普通的表单,不需要任何特殊的标记:

. 代码如下:


<form id="myform" method="post" action="/home/ajaxform">
<p>
name:<input id="username" name="username" type="text" />  
password:<input id="password" name="password" type="text" />
<br />
<input type="submit" value="submit async" id="lnksubmit" />
</p>
</form>


在没有jquery.form的时候,提交表单,页面会进入阻塞模式,等待服务器端的响应。

 

2. 引入jquery和form plugin javascript脚本文件并且添加几句简单的代码让页面在dom加载完成后初始化表单:

<head>    
<script type="text/javascript" src="path/to/jquery.js"></script>    
<script type="text/javascript" src="path/to/form.js"></script>     
<script type="text/javascript">        
// wait for the dom to be loaded        
$(document).ready(function() {            
// bind 'myform' and provide a simple callback function            
// 为myform绑定ajaxform异步提交事件,并提供一个简单的回调函数。           
$('#myform').ajaxform(function() {                
alert("thank you for your comment!");            
});        
});    
</script>
</head>
加上jquery.form组件后,提交表单时,页面不会再同步提交,而是由js做异步提交,因此提交后页面不会有刷新。

3. 加入能够与服务器端进行交互的回调函数。

. 代码如下:


$(document).ready(function () {
     //options是一个ajaxform的配置对象。?
     var options = {
        //target: '#output1',   // target element(s) to be updated with server response 
        //beforesubmit: showrequest,  // pre-submit callback 
       <font color=#ff0000> success: callbackfunc  // post-submit callback</font> 

        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //datatype:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //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 
    };

    // bind form using 'ajaxform' 
    $('#myform').ajaxform(options);
}); 

 

 // responsetext是服务端的响应值。statustext是页面

 // 提交状态值,success表示成功。
function callbackfunc(responsetext, statustext) {
    if (statustext == 'success') {
        alert(responsetext);
    }

 else{

 alert(“服务端错误!”);

      }
}

如果返回的是json数据则回调函数可以这么写
function resultfunction(responsetext,statustext) {
        if (statustext == 'success') {
            if (responsetext.code == 1) {
                alert(responsetext.message);
            } 
            else {
                alert('error occurs!');
            }
        }
        else {
            alert('服务器错误!');
        }
    }


服务端的代码如下:

. 代码如下:


[httppost]
public actionresult ajaxform(formcollection form)
{
    string message = "name:" + form["username"] + " pwd: "+form["password"]  ;
    //return content(message);
    return json(new { code = 1, message = message });
}


4. 加入提交前的数据校验函数
为options对象添加 beforesubmit属性

. 代码如下:


var options = {
                //target: '#output1',   // target element(s) to be updated with server response 
                <font color=#ff0000>beforesubmit: checkdata,  // pre-submit callback 
</font>                success: callbackfunc  // 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 
                //datatype:  null        // 'xml', 'script', or 'json' (expected server response type) 
                //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 
            };
 // pre-submit callback 
       function checkdata(formdata, jqform, options) {
           // formdata is an array; here we use $.param to convert it to a string to display it 
           // but the form plugin does this for you automatically when it submits the data 
           //var querystring = $.param(formdata);

           // jqform is a jquery object encapsulating the form element.  to access the 
           // dom element for the form do this: 
           var formelement = jqform[0]; 

           //alert('about to submit: \n\n' + querystring);

           // here we could return false to prevent the form from being submitted; 
           // returning anything other than false will allow the form submit to continue 
           //return true;
           if ($(formelement).find("#username").val() == "") {
               alert("please enter username!");
               return false;
           } else {
               return true;
           }
       }


验证用户名是否为空,是则提示输入,并取消表单提交。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网