Home php教程 php手册 php实现input输入框失去焦点自动保存输入框的数据

php实现input输入框失去焦点自动保存输入框的数据

May 25, 2016 pm 04:44 PM
Auto save

最近做一个输入框失去焦点时自动保存数据的功能,当然就是jQuery选择器选择input,blur时,ajax提交数据给php文件,php文件保存一下数据,主要是要注意一下中文的问题,所以中间需要转一下编码.

下面的实例是一个列表页,有一点类似excel了,html代码如下:

<table width="100%" align="left" border="0" cellspacing="0" bgcolor="#CCCCCC"> 
<tr onmouseover="this.className=&#39;on&#39;;" onmouseout="this.className=";" align="center" id="{$id}" bgcolor="#FFFFFF"> 
<td >{$id}</td> 
<td ><input type="text" id="title_{$id}" value="{$title}"  onblur="ajaxEdit(&#39;{$id}&#39;,&#39;title&#39;);" ></td> 
<td ><textarea id="description_{$id}"  rows="4"  onblur="ajaxEdit(&#39;{$id}&#39;,&#39;description&#39;);" >{$description}</textarea></td> 
<td ><a href="view.php?aid={$id}" target="_blank">预览</a><a href="edit.php?aid={$id}">编辑</a></td> 
</tr> 
</table>
Copy after login

js代码如下:

<script type="text/javascript"> 
    function Dd(i) {
        return document.getElementById(i);
    }
    function ajaxEdit(aid, field) {
        var value = decodeURI(Dd(field + "_" + aid).value);
        $.ajax({
            type : "POST",
            url : "edit.php",
            data : {
                dopost : "ajaxSave",
                aid : aid,
                field : field,
                value : value
            },
            success : function (data) {
                if (data == "true") { //更新成功
                    Dd(field + "_" + aid).style.border = "#fff";
                } else { //更新失败
                    alert("更新失败,可能是网速太慢");
                }
            }
        });
    }
</script>
Copy after login

php代码,引用了dedecms的函数、方法,代码如下:

if($dopost==&#39;ajaxSave&#39;) 
{ 
    $value = urldecode(AutoCharset($value,"UTF-8","GB2312")); 
    //更新主表 
    $inQuery = "UPDATE `#@__archives` SET $field=&#39;$value&#39; WHERE id=&#39;$aid&#39;"; 
    if($dsql->ExecuteNoneQuery($inQuery)) 
    { 
echo "true" ; 
exit; 
    }else{ 
echo "false"; 
exit; 
    } 
 }
Copy after login

方法二,定时保存草稿功能,防止数据意外丢失.

情况介绍,网站后台编辑器是采用了百度UEditor所见即所得编辑器,我们就是要实现自动定时保存编辑器里的内容.

编辑器的调用方式如下:

<script type="text/plain" id="content" name="content" style="width:800px;height:400px;"></script> 
    <script type="text/javascript"> 
        var editor = new UE.ui.Editor(); 
        editor.render(&#39;content&#39;); 
    </script>
Copy after login

在发表页面或编辑页的最后加入js调用标签:

<script type="text/javascript" src="localStorag.js"></script>
Copy after login

localStorag.js 的代码如下:

/*注意: 本js脚本请在网页源代码最后的地方放置*/ 
if (!window.localStorage) {
    alert(&#39;您的浏览器不支持 localStorage 技术!&#39;);
} else {
    var spanObj = document.getElementById(&#39;s1&#39;);
    var saveTimer = setInterval(function () {
            var str = "";
            if (document.all) {
                /*IE*/
                str = document.frames[1].document.body.innerHTML;
            } else {
                /*Chrome,ff*/
                str = document.getElementById("ueditor_0").contentDocument.body.innerHTML;
            }
            if (str.length > 20 && (str.indexOf("。") > -1 || str.indexOf(",") > -1)) {
                /*有内容才保存 且有句号或逗号*/
                localStorage.setItem("ctValue", str);
                var d = new Date();
                var YMDHMS = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
                spanObj.innerText = &#39;(数据保存于: &#39; + YMDHMS + &#39;)&#39;;
                setTimeout(function () {
                    spanObj.innerText = &#39;&#39;;
                }, 5000);
            }
        }, 25000); //每隔N秒保存一次
    function stoplocs() {
        clearInterval(saveTimer); //停止保存
        //localStorage.removeItem("ctValue"); //清空
    }
    function showlocs() {
        var html = localStorage.getItem("ctValue");
        editor.setContent(html);
        //alert(localStorage.getItem("ctValue"));
    }
}
Copy after login

可以增加停止保存按钮或立即恢复按钮,如下:

<a href="javascript:void(0)" onclick="javascript:stoplocs()">停止保存</a> 
<a href="javascript:void(0)" onclick="javascript:showlocs()">立即恢复</a>
Copy after login

好了,现在你的网站发表页面/编辑页面就具备自动保存功能了,注意这种方式支持IE8及以上版本的IE,CHROME,Firefox等等大多数主流浏览器,至于IE6,实在是老掉牙了,不在考虑范围内,如果一定要考虑IE6的,请使用上面提供的IE6/ie7兼容方案.


本文地址:

转载随意,但请附上文章地址:-)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)