javascript代码实例教程-利用jquery+iframe做一个ajax上传效果

发布时间:2019-04-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-利用jquery+iframe做一个ajax上传效果脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

  以下是自学IT网--中级班上课笔记

  

htML页面


[html]
<!DOCTYPE html PubLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html XMlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> 
<head> 
<title>利用jquery+iframe做一个ajax上传效果</title> 
<;meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
<meta name="description" content="" /> 
<meta name="keywords" content="" /> 
<script src="http://libs.baidu.COM/jquery/1.8.2/jquery.min.js"></script> 
<script type="text/javascript"> 
// 利用 jquery+iframe 做一个ajax上传效果 
 
/* 
思路: 
1: 点击"提交"时的瞬间,生成一个iframe对象,插入body中 
2: 修改form的target ,为iframe的name值 
3: 给iframe加1个事件 ,onload 
*/ 
 
$( 
    function() { 
        $(&#39;input:button').click(function(){ 
            //alert('s'); 
            VAR ifmname = 'ifm' + Math.random(); 
            var ifm = $('<iframe width="0" height="0" frameborder="0" name="'+ ifmname +'">'); 
            ifm.appendTo($('body')); 
 
            $('form').attr('target',ifmname); 
            $('form').submit(); 
 
            $('#PRogress').html('<img src=http://up.2cto.com/2013/0619/20130619015352786.gif" border="0">'); 
            ifm.load(function(){ 
                $('#progress').html('上传完毕'); 
                this.remove(); 
            }); 
 
        });  
    } 
); 
 
 
</script> 
<style type="text/css"> 
</style> 
</head> 
    <body> 
        <p id="progress"></p> 
        <form action="upfile.php" method="post" enctype="multipart/form-data" target="upfile"> 
            <input type="file" name="pic" /><br /> 
            <input type="button" value="提交" /> 
        </form>         
    </body> 
</html> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>利用jquery+iframe做一个ajax上传效果</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script src="http://libs.baidu.com/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
// 利用 jquery+iframe 做一个ajax上传效果

/*
思路:
1: 点击"提交"时的瞬间,生成一个iframe对象,插入body中
2: 修改form的target ,为iframe的name值
3: 给iframe加1个事件 ,onload
*/

$(
    function() {
        $('input:button').click(function(){
            //alert('s');
            var ifmname = 'ifm' + Math.random();
            var ifm = $('<iframe width="0" height="0" frameborder="0" name="'+ ifmname +'">');
            ifm.appendTo($('body'));

            $('form').attr('target',ifmname);
            $('form').submit();

            $('#progress').html('<img src=http://up.2cto.com/2013/0619/20130619015352786.gif" border="0">');
            ifm.load(function(){
                $('#progress').html('上传完毕');
                this.remove();
            });

        });
    }
);


</script>
<style type="text/css">
</style>
</head>
    <body>
        <p id="progress"></p>
        <form action="upfile.php" method="post" enctype="multipart/form-data" target="upfile">
            <input type="file" name="pic" /><br />
            <input type="button" value="提交" />
        </form>       
    </body>
</html>
upfile.php


[php]
echo move_uploaded_file($_FILES['pic']['tmp_name'],'./upload/' . $_FILES['pic']['name']) ? 'OK':'fail'; 

echo move_uploaded_file($_FILES['pic']['tmp_name'],'./upload/' . $_FILES['pic']['name']) ? 'OK':'fail';


 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-利用jquery+iframe做一个ajax上传效果全部内容,希望文章能够帮你解决javascript代码实例教程-利用jquery+iframe做一个ajax上传效果所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。