0
我想用数组创建一个对象,每个对象应该有文件名的数组。我将该对象命名为productid
,它可以有多个文件名。当客户给出productid
时,文件名必须显示在文本区域中。我有以下代码:localstorage中存在数组的对象
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<script type="text/javascript">
var productid = []
var filename = []
function managerClick(){
console.log("manager", productid);
console.log("manager", filename);
productid.push(document.getElementById("productId").value);
filename.push(document.getElementById("names").value);
localStorage.setItem("filename", JSON.stringify(filename));
localStorage.setItem("productid", JSON.stringify(productid));
var result={}
productid.map(function(k){
result[k]=filename;
})
console.log(result);
document.getElementById('myTextarea').value = "";
document.getElementById('CustomerpId').value = "";
};
function customerClick(){
document.getElementById('myTextarea').value = "";
var CustomerpId = document.getElementById('CustomerpId').value;
var filenames = JSON.parse(localStorage.getItem("filename"));
var productIds= JSON.parse(localStorage.getItem("productid"));
var tempArr = [];
for(i=0; i< productIds.length; i++) {
if(productIds[i] == CustomerpId) {
tempArr.push(i);
}
}
for(i=0; i< tempArr.length; i++) {
document.getElementById('myTextarea').value += filenames[tempArr[i]] + ",";
}
};
</script>
<body>
<div class="w3-card-4 w3-margin" style="width:50%;">
<center>Manager</center>
<div class="w3-container">
Product Id: <input type="text" id="productId"><br></br>
File Name: <input type="text" id="names"><br></br>
<center><button class="w3-btn w3-dark-grey" onclick="managerClick()">Data Entered</button></center><br>
</div>
<center>Customer</center>
<div class="w3-container">
Product Id: <input type="text" id="CustomerpId"><br></br>
<center>
<button class="w3-btn w3-dark-grey" onclick="customerClick()">Click To get filename</button>
</center><br>
<textarea rows="4" cols="30" id="myTextarea"></textarea>
</div>
</div>
</body>
</html>
这段代码的问题是,每当我进入我productid
在我的客户部分,正显示所有文件名的数组。我想显示仅特定productid
阵列:
example: product1[ file1,file2,file3]
product2[ file1,file2,file3,file4]
product3[ file1]
在该产品的阵列应当被显示,如果特定的乘积给出,然后在该产品中的数据已经被显示,上述代码显示它像此:
输入我给1作为产品ID “A,AA,AAA”,2AS第二产品ID和 “b,BB,BBB” 作为文件名。在我的控制台中,aa,aaa和b,bb,bbb都显示在两个产品中,我不希望发生这种情况。所有a,aa,aaa都应该在第一个产品中,并且所有的b,bb,bbb都应该保存在第二个产品中。每个产品都应该显示自己的价值观。
这个工程,但同时保存自己,我需要保存,因为我在该示例中给出的例子:product1 [file1,file2,file3] product2 [file1,file2,file3,file4] product3 [file1]。你能帮助我吗? –
即将到来的问题是什么?我刚刚更新了你的'customerClick'代码。 – Apb
检查更新的图像 –