jQuery实现Ajax提交form表单的简单方法

6 篇文章 0 订阅
1 篇文章 0 订阅

转载自:http://www.okbase.net/doc/details/2539

介绍:介绍了如何将一个普通的form表单,迅速改造成通过ajax方式提交,并将结果显示在对话框中。
正文:
我们有一个非常普通的表单:
<form id="form1" name="form1" method="get" action="post.html">
标题<input id="testtitle" name="testtitle" type="text" size="40" />
    <input type="submit" value="提交">
</form>

为了演示方便,method使用了get,可以根据需要改为post。

现在我们把它改造成AJAX方式提交,方法很简单,只需要将下面的代码复制到页面中:

<html>
<body>
<link type="text/css" href="jquery-ui.css" rel="stylesheet" />
<style>
#loading{background-image:url(images/loading.gif);background-position:0px 0px;background-repeat:no-repeat; position:absolute;width:50px;height:50px;top:60%;left:50%;margin-left:-25px;text-align:center;}
</style>
<script type ="text/javascript" src="jquery.js"></script>
<script type ="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript" src="jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
    bindSubmit();
    $("#loading").hide();
    $("#msgdlg").hide();
});

function bindSubmit() {
    var options = {
            target: '#msgdlg',
            success: showResponse,
			error: showError

            // 其它可选参数: 
            //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 选项,例如超时: 
            //timeout:   3000 
        };

        $('#form1').submit(function () {
            $(this).ajaxSubmit(options);
			$("#loading").show();
            return false;
        });
}

// 
function showResponse(responseText, statusText, xhr, $form) {
    $("#loading").hide();
	messagebox(responseText);
    //bindSubmit();
}

function showError(xhr, ajaxOptions, thrownError) {
    $("#loading").hide();
	messagebox("出错了!" + thrownError);
    bindSubmit();
}

// 显示结果信息的对话框
function messagebox(msg) {
    $("#msgdlg").html(msg);
    $("#dialog:ui-dialog").dialog("destroy");
    $("#msgdlg").dialog({
            modal: true,
            width: 380,
            height: 230,
            buttons: {
                确认: function () {
                    $(this).dialog("close");
                }
            }
    });
}
</script>


<form id="form1" name="form1" method="get" action="post.html">
标题<input id="testtitle" name="testtitle" type="text" size="40" />
<input type="submit" value="提交">
</form>
<div id="msgdlg" title="消息"></div>
<div id="loading" style="display:none" οndblclick="this.style.display='none'"></div>
</body>
</html>

代码中关键的几点:

(1)$('#form1').submit()对Form的Submit的绑定 (使用了jquery.form.js)

(2)定义的showResponse函数处理返回信息。

(3)$("#msgdlg").dialog()创建对话框并显示结果 (使用了jquery-ui.js)

 下载源代码:http://www.okbase.net/file/item/2998





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值