抱歉,我无法详细说明我的问题,但我看不到任何问题。这两个函数save()
并load()
运行onclick
<button onclick="save()">save</button>
<button onclick="load()">load</button>
中的代码用于获取所有输入的值并将save
其存储到本地存储中。然后load
将保存的数据重新添加到表单字段中
我是php和JavaScript的新手,所以它看起来可能有点混乱,但这是因为我通过php数组和for循环“生成”了该数组(数组的选择取决于with选项)
//救
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
// 加载
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
$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代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句