本地存储不工作?不知道为什么

时间:2014-12-03 05:03:47

标签: javascript php local-storage

抱歉,我无法详细说明我的问题,但我真的看不出任何问题。这两个函数save()load()运行onclick

<button onclick="save()">save</button>
<button onclick="load()">load</button>

代码用于获取所有输入的值,并将save获取到本地存储。然后在load上将保存的数据添加回表单字段

我是php和JavaScript的新手所以它可能看起来有点乱,但这是因为我生成了#34;这是通过php数组和for循环(数组根据选项的变化而选择)

//保存

function save() {
if (typeof(Storage) != "undefined") {
        var seometatitle = document.getElementById("seo_meta_title").value;
            saveData(seometatitle);

        function saveData(xseometatitle) {
                localStorage.setItem("seometatitle", xseometatitle);
        }
    }

        var seometadescription = document.getElementById("seo_meta_description").value;
            saveData(seometadescription);

        function saveData(xseometadescription) {
            localStorage.setItem("seometadescription", xseometadescription);
        }   
    }

        var header = document.getElementById("header").value;
            saveData(header);

        function saveData(xheader) {
            localStorage.setItem("header", xheader);
        }
    }

        var pagetext = document.getElementById("page_text").value;
            saveData(pagetext);

        function saveData(xpagetext) {
            localStorage.setItem("pagetext", xpagetext);
        }
    }

        var linkurl = document.getElementById("link_url").value;
            saveData(linkurl);

        function saveData(xlinkurl) {
            localStorage.setItem("linkurl", xlinkurl);
        }   
    }

        var buttontext = document.getElementById("button_text").value;
            saveData(buttontext);

        function saveData(xbuttontext) {
            localStorage.setItem("buttontext", xbuttontext);
        }   
    }
}} //end

// LOAD

function load() {
if (typeof(Storage) != "undefined") {
document.getElementById("seo_meta_title").value = localStorage.getItem("seometatitle");
document.getElementById("seo_meta_description").value =  localStorage.getItem("seometadescription");
document.getElementById("header").value = localStorage.getItem("header");
document.getElementById("page_text").value = localStorage.getItem("pagetext");
document.getElementById("link_url").value = localStorage.getItem("linkurl");
document.getElementById("button_text").value = localStorage.getItem("buttontext");
}}

只是为了它,这里是php生成save function

$arrlength = count($matches[0]); # Get how many items in array two.
for($x = 0; $x < $arrlength; $x++) { #counting
    $place_name = str_replace ( "_" , "" , $matches[0][$x] ); #Create readible name _ to space.
    echo 'var ' .$place_name. ' = document.getElementById('.'"'.$matches[0][$x].'"'.').value;';

        echo "\n";
        echo   'saveData('.$place_name.');';
        echo "\n";
        echo 'function saveData(x'.$place_name.') {';
        echo "\n";
        echo 'localStorage.setItem("'.$place_name.'", x'.$place_name.');';
        echo "\n";
        echo '}}';
        echo "\n";

这是load function

的php
$arrlength = count($matches[0]); # Get how many items in array two.
for($x = 0; $x < $arrlength; $x++) { #counting
    $place_name = str_replace ( "_" , "" , $matches[0][$x] ); #Create readible name _ to space.
     // Retrieve
    echo 'document.getElementById("'.$matches[0][$x].'").value = localStorage.getItem("'.$place_name.'");';
    echo "\n"; }

它使用的数组是,它是通过抓取{placeholder}自动制作的.html文件,占位符根据使用的html文件而改变

   array(1) { 
    [0]=> array(6) { 
    [0]=> string(12) "placeholders" 
    [1]=> string(15) "page_meta_title" 
    [2]=> string(6) "header" 
    [3]=> string(9) "page_text" 
    [4]=> string(8) "link_url" 
    [5]=> string(11) "button_text" } 
    }
    }

任何帮助将不胜感激!谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

您有相同功能的多个定义。只有最后一个定义才有效。在运行任何javascript之前,所有的javascript都会被解析。因此,当您尝试多次调用它时,将解析所有saveData()定义,并且只有最后一个定义处于活动状态。因此,只有最后一个执行。

很难阅读您的代码或理解您没有代码缩进的意图,但您应该有一个saveData()函数和一个loadData()函数,只需多次调用它们,每个时间不同的论点。

我建议您手动编写javascript代码,然后使用您的PHP生成Javascript代码可以使用的数据结构,而不是生成Javascript代码。

相关问题