简体   繁体   English

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

[英]Local Storage Not working? Have no idea why

Sorry i couldn't elaborate on my problem more but i can't really see any problems. 对不起,我无法详细说明我的问题,但我看不到任何问题。 the two functions save() and load() run onclick 两个函数save()load()在onclick上运行

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

The code in meant to grab the value of all the inputs and save it to local storage. 中的代码用于获取所有输入的值savesave到本地存储中。 then on load add the saved data back into the form Fields 然后在load将保存的数据添加回表单字段

i'm new to php and JavaScript so it may look a bit messy but that's because i "generate" this via a php array and for loop (the array changes depending on with option is chosen) 我是php和JavaScript的新手,所以它看起来可能有点混乱,但这是因为我通过php数组和for循环“生成”了该数组(数组的选择取决于选择的选项)

//save //救

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");
}}

Just for the sake of it here is the php to generate the save function 仅仅为了它,这里是生成save 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.
    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";

And here is the php for the load function 这是用于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"; }

The array it's using is, its made automatically by grabbing {placeholder} form a .html file the place holders change depending on the html file used 它使用的数组是通过抓取{placeholder}形式的.html文件自动生成的, {placeholder}根据所使用的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" } 
    }
    }

Any help would be greatly appreciated! 任何帮助将不胜感激! thanks for your time. 谢谢你的时间。

You have multiple definitions of the same function. 您具有相同功能的多个定义。 Only the last definition will be in force. 仅最后一个定义生效。 All your javascript is parsed before any of it is run. 在运行任何JavaScript之前,都将对其进行解析。 So, all the saveData() definitions are parsed and only the last one is active when you then try to call it multiple times. 因此,将解析所有saveData()定义,并且当您尝试多次调用它时,只有最后一个处于活动状态。 Thus, only the last one ever executes. 因此,只有最后一个执行。

It's hard to read your code or understand your intent with no code indentation, but you should probably have one saveData() function and one loadData() function and just call them multiple times, each time with different arguments. 没有代码缩进就很难阅读代码或理解意图,但是您可能应该拥有一个saveData()函数和一个loadData()函数,并多次调用它们,每次使用不同的参数。

I would recommend that you write your javascript code manually and then generate data structures with your PHP that the Javascript code can use, NOT generate Javascript code. 我建议您手动编写javascript代码,然后使用PHP生成Javascript代码可以使用的数据结构,而不是生成Javascript代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM