Javascript 本地存储不工作?不知道为什么
对不起,我不能详细说明我的问题,但我真的看不到任何问题。这两个函数Javascript 本地存储不工作?不知道为什么,javascript,php,local-storage,Javascript,Php,Local Storage,对不起,我不能详细说明我的问题,但我真的看不到任何问题。这两个函数save()和load()运行onclick <button onclick="save()">save</button> <button onclick="load()">load</button> //装载 function load() { if (typeof(Storage) != "undefined") { document.getElementById("seo_m
save()
和load()
运行onclick
<button onclick="save()">save</button>
<button onclick="load()">load</button>
//装载
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");
}}
为了方便起见,这里是生成save函数的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";
$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" }
}
}
任何帮助都将不胜感激!谢谢您的时间。您对同一函数有多个定义。只有最后一个定义才有效。在运行任何javascript之前,都会对其进行解析。因此,所有的saveData()
定义都将被解析,当您尝试多次调用它时,只有最后一个定义处于活动状态。因此,只有最后一个执行过
在没有代码缩进的情况下,很难阅读代码或理解您的意图,但您可能应该有一个saveData()
函数和一个loadData()
函数,并且只需多次调用它们,每次都使用不同的参数
我建议您手动编写javascript代码,然后使用PHP生成javascript代码可以使用的数据结构,而不是生成javascript代码。请正确缩进代码以使其可读。感谢您的回答,是的,这就是我遇到的问题。因此,在本例中,如果我将saveData()
更改为'saveData.$place\u name.'(),
和loadData相同,它应该可以工作吗?我将尝试用JavaScript编写它,因为数组转换没有真正起作用,所以我改为这种方法。谢谢你
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" }
}
}