发布三个ajax相关的函数包括无刷新提交表单等

2016-02-19 12:58 1 1 收藏

下面是个发布三个ajax相关的函数包括无刷新提交表单等教程,撑握了其技术要点,学起来就简单多了。赶紧跟着图老师小编一起来看看吧!

【 tulaoshi.com - Web开发 】

几个月前,因为项目需求,我写了下面的三个ajax相关的函数。发布出来和大家分享。
第一个是用来无刷新加载一段HTML
第二个是把表单数据转换成一串请求字符串
第三个是结合函数一和函数二的无刷新提交表单实现。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

还有一点要提到的是,无刷新表单提交,还不能对文件上传进行处理,这个主要是因为浏览器的安全设置。目前无刷新的上传,一般是用iframe来实现的。关于这个,我们在google里搜索能找到很多。

网上虽然已经有很多优秀的ajax的类和函数了,但是或许我这几个函数对大家还有点用处,于是我就发布出来了。
可以在这里下载。
代码如下:

//@desc    load a page(some html) via xmlhttp,and display on a container
//@param   url          the url of the page will load,such as "index.php"
//@param   request      request string to be sent,such as "action=1&name=surfchen"
//@param   method       POST or GET
//@param   container          the container object,the loaded page will display in container.innerHTML
//@usage 
//         ajaxLoadPage('index.php','action=1&name=surfchen','POST',document.getElementById('my_home'))
//         suppose there is a html element of "my_home" id,such as "span id='my_home'/span" 
//@author  SurfChen surfchen@gmail.com
//@url     http://www.surfchen.org/
//@license http://www.gnu.org/licenses/gpl.html GPL
function ajaxLoadPage(url,request,method,container)
{
    method=method.toUpperCase();
    var loading_msg='Loading...';//the text shows on the container on loading.
    var loader=new XMLHttpRequest;//require Cross-Browser XMLHttpRequest
    if (method=='GET')
    {
        urls=url.split("?");
        if (urls[1]=='' || typeof urls[1]=='undefined')
        {
            url=urls[0]+"?"+request;
        }
        else
        {
            url=urls[0]+"?"+urls[1]+"&"+request;
        }

        request=null;//for GET method,loader should send NULL
    }
    loader.open(method,url,true);
    if (method=="POST")
    {
        loader.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    }
    loader.onreadystatechange=function(){
        if (loader.readyState==1)
        {
            container.innerHTML=loading_msg;

        }
        if (loader.readyState==4)
        {
            container.innerHTML=loader.responseText;
        }
    }
    loader.send(request);
}
//@desc    transform the elements of a form object and their values into request string( such as "action=1&name=surfchen")
//@param   form_obj          the form object
//@usage   formToRequestString(document.form1)
//@notice  this function can not be used to upload a file.if there is a file input element,the func will take it as a text input.
//         as I know,because of the security,in most of the browsers,we can not upload a file via xmlhttp.
//         a solution is iframe.
//@author  SurfChen surfchen@gmail.com
//@url     http://www.surfchen.org/
//@license http://www.gnu.org/licenses/gpl.html GPL
function formToRequestString(form_obj)
{
    var query_string='';
    var and='';
    //alert(form_obj.length);
    for (i=0;iform_obj.length ;i++ )
    {
        e=form_obj[i];
        if (e.name!='')
        {
            if (e.type=='select-one')
            {
                element_value=e.options[e.selectedIndex].value;
            }
            else if (e.type=='checkbox' || e.type=='radio')
            {
                if (e.checked==false)
                {
                    break;    
                }
                element_value=e.value;
            }
            else
            {
                element_value=e.value;
            }
            query_string+=and+e.name+'='+element_value.replace(/&/g,"%26");
            and="&"
        }

    }
    return query_string;
}
//@desc    no refresh submit(ajax) by using ajaxLoadPage and formToRequestString
//@param   form_obj          the form object
//@param   container          the container object,the loaded page will display in container.innerHTML
//@usage   ajaxFormSubmit(document.form1,document.getElementById('my_home'))
//@author  SurfChen surfchen@gmail.com
//@url     http://www.surfchen.org/
//@license http://www.gnu.org/licenses/gpl.html GPL
function ajaxFormSubmit(form_obj,container)
{
    ajaxLoadPage(form_obj.getAttributeNode("action").value,formToRequestString(form_obj),form_obj.method,container)
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1602633.html

延伸阅读
标签: Web开发
做公司网站时的一个应用,用猫东的表单验证插件做实时的验证,和jquery的ajax提交数据,获取评论的时候加入简单的分页 原理很简单,注意一下编码问题就行了 实现了获取评论无刷新,发表评论无刷新,页面获取评论时显示loading加载效果 jquery真的是一个非常优秀的JS库,简单容易掌握,对于网页中的多级菜单、级联效果、Tab...
标签: Web开发
将index.html和upload.php文件保存到apache工作目录,例子使用安装目录D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/ AJAX 客户端页面代码: index.html html  body  h1Ajax file upload sample/h1br/input id="uplaod" name="btn_send" type="button" value="上传测试"/  div id=result/div  PRE...
标签: Web开发
HTML HEAD titleAjax实现无刷新三联动下拉框/title meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR" meta content="C#" name="CODE_LANGUAGE" meta content="JavaScript" name="vs_defaultClientScript" meta content="http://schemas.microsoft.com/intellis...
标签: Web开发
request.jsp页面中有rocarsId,和ccrn两个text。 对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn的值,ajax提交到response.jsp页面,并调用RocarsEntiy.updateCcrn方法更新对应的ccrn,最后无刷新显示 代码: request.jsp 代码如下: %@ page language="java" contentType="text/html; charset=ISO-8859-1"...
标签: Web开发
之前实现AJAX使用Javascript脚本一个一个敲出来的,很繁琐。学习Jquery之后就感觉实现AJAX并不是那么的困难了,当然除了Jquery框架外还有其它的优秀框架这里我就着重说下比较流行的Jquery。Jquery AJAX提交表单有两种方式,一是url参数提交数据,二是form提交(和平常一样在后台可以获取到Form表单的值)。在所要提交的表单中,如果元素很多的话...

经验教程

828

收藏

77
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部