2016-03-15 57 views
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] 

在該產品的陣列應當被顯示,如果特定的乘積給出,然後在該產品中的數據已經被顯示,上述代碼顯示它像此:

enter image description here

輸入我給1作爲產品ID 「A,AA,AAA」,2AS第二產品ID和 「b,BB,BBB」 作爲文件名。在我的控制檯中,aa,aaa和b,bb,bbb都顯示在兩個產品中,我不希望發生這種情況。所有a,aa,aaa都應該在第一個產品中,並且所有的b,bb,bbb都應該保存在第二個產品中。每個產品都應該顯示自己的價值觀。

回答

0

試試這個功能:

 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]] + ","; 
     }   
    }; 

在單擊獲取字段名稱的,你必須得到您想要顯示,然後在產品ID陣列和陣列的文件名執行操作的ID。

編輯:每正如你在評論中提到這裏是更新的代碼:

function managerClick() { 
     var productid = document.getElementById("productId").value; 
     var filename = document.getElementById("names").value; 

     var oldItems = JSON.parse(localStorage.getItem(productid)) || []; 
     var newItem = filename; 
     oldItems.push(newItem); 
     localStorage.setItem(productid, JSON.stringify(oldItems)); 
    } 

    function customerClick() { 
     document.getElementById('myTextarea').value = ""; 
     var CustomerpId = document.getElementById('CustomerpId').value; 
     var productIds = JSON.parse(localStorage.getItem(CustomerpId)); 
     for(i=0;i<productIds.length;i++) { 
      document.getElementById('myTextarea').value += productIds[i] + ","; 
     } 

    } 

希望這有助於。

+0

這個工程,但同時保存自己,我需要保存,因爲我在該示例中給出的例子:product1 [file1,file2,file3] product2 [file1,file2,file3,file4] product3 [file1]。你能幫助我嗎? –

+0

即將到來的問題是什麼?我剛剛更新了你的'customerClick'代碼。 – Apb

+0

檢查更新的圖像 –

相關問題