使用jquery+iframe做一個ajax上傳效果(實例)
更新時間:2017年08月24日 10:04:41 投稿:jingxian
下面小編就為大家?guī)硪黄褂胘query+iframe做一個ajax上傳效果(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
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="<img src="http://img.jbzj.com/file_images/article/201708/loading.gif" alt="" />" border="0">'); ifm.load(function(){ $('#progress').html('上傳完畢'); this.remove(); }); }); } ); </script> <style type="text/css"> </style> </head> <body> <div id="progress"></div> <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
echo move_uploaded_file($_FILES['pic']['tmp_name'],'./upload/' . $_FILES['pic']['name']) ? 'OK':'fail';
以上這篇使用jquery+iframe做一個ajax上傳效果(實例)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JQERY limittext 插件0.2版(長內(nèi)容限制顯示)
JQERY limittext 插件為長內(nèi)容增加一個顯示更多的功能2010-08-08jQuery函數(shù)的第二個參數(shù)獲取指定上下文中的DOM元素
這篇文章主要介紹了jQuery函數(shù)的第二個參數(shù)獲取指定上下文中的DOM元素,需要的朋友可以參考下2014-05-05jQuery Attributes(屬性)的使用(二、類篇)
本系列文章主要講述jQuery框架的屬性(Attributes)使用方法,我將以實例方式進行講述,以簡單,全面為基礎(chǔ),不會涉及很深,我的學習方法:先入門,后進階!2009-12-12