【发布时间】:2014-08-20 14:14:24
【问题描述】:
我正在尝试在 android 中使用 phonegap-1.4.1 开发条形码扫描应用程序。我试图将所有值存储在数组code[] 中,稍后我将使用数组显示值。我正在创建一个local storage 的值。这是我对数组和计数器的声明。
localStorage["counter"]=0;
var code = ["Test1", "Test2","Test3", "Test4"];
localStorage.setItem("code", JSON.stringify(code));
这是我用于扫描条形码的 javascript 文件,我在其中使用递归函数,以便应用程序继续扫描并将值存储在数组中。
var scanCode = function () {
window.plugins.barcodeScanner.scan(
function (result) {
if(result.cancelled == true ) {
window.location.href = 'page5.html';
} else {
var test2 = JSON.parse(localStorage.getItem("code"));
var k = parseInt(localStorage.getItem("counter"));
document.getElementById(test2[k]).innerHTML = "result.text";
k++;
localStorage["counter"] = k;
alert("Scanned Code: " + result.text + ". Format: " + result.format + ". Cancelled: " + result.cancelled);
scanCode();}
}, function (error) {
alert("Scan failed: " + error);
window.location.href = 'page5.html';
});
}
在存储值之后,我将某些 page5.html 中的值显示为
<tr>
<td >1</td>
<td>
<p id="Test1"></p></td>
</tr>
但我收到的错误是BarcodeScanner2 = TypeError: Cannot set property 'innerHTML' of null。有人请在这个问题上帮助我。提前致谢。
【问题讨论】:
标签: javascript html arrays cordova local-storage