2013-05-19 52 views
4

我試圖在javascript中從localstorage中拉出一串數據,併合並多個字符串的多個值。例如 我可以具有以下2個字符串從本地存儲在javascript中檢索多個字符串值

Bob;companyA;6141692120;[email protected];1 John St;;Bellevue;6056;;6;10;20.00;52.800000000000004;72.80;7.28;80.08; 
Jane;companyB;6157692120;[email protected];1 Jack St;;Bellevue;6056;;6;10;20.00;52.800000000000004;72.80;7.28;80.08; 

我想忽略前幾個和在串中的字符串添加的最後一個5或6一起從每個,所以值[13] A +值[13] B = X,會有大量的字符串,它們都有隨機數字,我附上了下面的JavaScript代碼,希望有人能指出我正確的方向。

windows.onload= getAllItems(); 
function getAllItems(){ 

var all = []; 
for(var i = 0, j = localStorage.length; i < j; i++) { 
    all[i] = localStorage[i].split(';'); 
} 

var sum = 0; 
for(var i = 0, j = all.length; i < j; i++) { 
    sum += all[i][12]; 
} 

var bags = all[9]; 
var distance = all[10]; 
var hdelivery_fee = all[11]; 
var hprice = all[12]; 
var htotal_notax = all[13]; 
var hgst = all[14]; 
var htotal_tax = all[15]; 
var hordernumber = all[16]; 

document.write('Number of Bags; ' + bags + '<br />'); 
document.write('Distance; ' + distance + '<br />'); 
document.write('Delivery Fee; $' + hdelivery_fee + '<br />'); 
document.write('Price of Bags; $' + hprice + '<br />'); 
document.write('Total Ex-GST; $' + htotal_notax + '<br />'); 
document.write('GST; $' + hgst + '<br />'); 
document.write('Total Inc GST; $' + htotal_tax + '<br />'); 
document.write('hordernumber; ' + hordernumber + '<br />'); 
} 

而且它可能是值得一提的是,這是一個任務,因此我不能使用JSON或jQuery的任何能力

編輯:我已經改變了代碼下面給出的答案匹配,而且我認爲我已經將輸出從循環中提取出來了,但是我非常喜歡這個新特性,所以很多都是碰撞和錯過。它似乎沒有工作,當是即使它看起來像它應該(對我來說)

EDIT2:

增加對位置整個HTML頁面:爲localStorage的信息源

function source() 
{ 
var newDate = new Date(); 
var itemId = newDate.getTime(); 
var values = new Array(); 
    var name = document.getElementById("name").value; 
    var company = document.getElementById("company").value; 
    var contactnumber= document.getElementById("contactnumber").value; 
    var email = document.getElementById("email").value; 
    var address1 = document.getElementById("address1").value; 
    var address2 = document.getElementById("address2").value; 
    var suburb = document.getElementById("suburb").value; 
    var postcode = document.getElementById("postcode").value; 
    var comments = document.getElementById("comments").value; 
    var bags = document.getElementById("bags").value; 
    var distance = document.getElementById("distance").value; 
    var hdelivery_fee = document.getElementById("hdelivery_fee").value; 
    var hprice = document.getElementById("hprice").value; 
    var htotal_notax = document.getElementById("htotal_notax").value; 
    var hgst = document.getElementById("hgst").value; 
    var htotal_tax= document.getElementById("htotal_tax").value; 
    var hordernumber= document.getElementById("hordernumber").value; 


values.push(name); 
values.push(company); 
values.push(contactnumber); 
values.push(email); 
values.push(address1); 
values.push(address2); 
values.push(suburb); 
values.push(postcode); 
values.push(comments); 
values.push(bags); 
values.push(distance); 
values.push(hdelivery_fee); 
values.push(hprice); 
values.push(htotal_notax); 
values.push(hgst); 
values.push(htotal_tax); 
values.push(hordernumber); 

try { 
    localStorage.setItem(itemId, values.join(";")); 
} catch (e) { 
    if (e == QUOTA_EXCEEDED_ERR) { 
     alert("Quota exceeded!"); 
    } 
} 

} 

EDIT3添加JavaScript的報告包括JavaScript。

<html> 
<head> 
<title>Form Processor</title> 
    <link rel="stylesheet" type="text/css" href="layout.css"> 




</head> 
<body> 
<div id="header"> 
     <h1 id="main_tile"> Everwarm Fuel Merchants - Daily Report </h1> 

    </div> 
<h1> Daily Sales Summary <h1> 

<p> 
<script> 
function getAllItems(){ 
    var all=[]; 
    for (i = 0,j=localStorage.length; i<j; i++) { 
     all[i] = localStorage.getItem(localStorage.key(i)).split(';'); 
    } 
    function getTotal(index) { 
     var sum = 0; 
     for(var i = 0, j = all.length; i < j; i++) { 
      sum += parseFloat(all[i][index]); 
     } 
     return sum; 
    } 
    var bags = getTotal(9); 
    var distance = getTotal(10); 
    var hdelivery_fee = getTotal(11); 
    var hprice = getTotal(12); 
    var htotal_notax = getTotal(13); 
    var hgst = getTotal(14); 
    var htotal_tax = getTotal(15); 
    var hordernumber = getTotal(16); 

    document.write('Number of Bags; ' + bags + '<br />'); 
    document.write('Distance; ' + distance + '<br />'); 
    document.write('Delivery Fee; $' + hdelivery_fee + '<br />'); 
    document.write('Price of Bags; $' + hprice + '<br />'); 
    document.write('Total Ex-GST; $' + htotal_notax + '<br />'); 
    document.write('GST; $' + hgst + '<br />'); 
    document.write('Total Inc GST; $' + htotal_tax + '<br />'); 
    document.write('hordernumber; ' + hordernumber + '<br />'); 
    }</script> 

    </p> 


<input type="button" value="Clear storage" onclick="localstorage.clear()" > </input> 

<input type="button" value="Return" onclick="history.back()"> </input> 

</body> 
</html> 
+0

+1:

試試這個。 – boisvert

+0

你不應該被允許使用'document.write'或者 – Alexander

+0

呵呵,我認爲使用'document.write'是新手的第一個指示 – user2397654

回答

1

您的解決方案使用一個包含所有過程的循環 - 即使是輸出! 您需要將過程分成兩部分。循環部分將拾取所有數據並將其全部添加,然後輸出部分不需要循環。對於大約是這之中的課程開放

function getAllItems(){ 
    var all=[]; 
    for (i = 0,j=localStorage.length; i<j; i++) { 
     all[i] = localStorage.getItem(localStorage.key(i)).split(';'); 
    } 
    function getTotal(index) { 
     var sum = 0; 
     for(var i = 0, j = all.length; i < j; i++) { 
      sum += parseFloat(all[i][index]); 
     } 
     return sum; 
    } 
    var bags = getTotal(9); 
    var distance = getTotal(10); 
    var hdelivery_fee = getTotal(11); 
    var hprice = getTotal(12); 
    var htotal_notax = getTotal(13); 
    var hgst = getTotal(14); 
    var htotal_tax = getTotal(15); 
    var hordernumber = getTotal(16); 

    document.write('Number of Bags; ' + bags + '<br />'); 
    document.write('Distance; ' + distance + '<br />'); 
    document.write('Delivery Fee; $' + hdelivery_fee + '<br />'); 
    document.write('Price of Bags; $' + hprice + '<br />'); 
    document.write('Total Ex-GST; $' + htotal_notax + '<br />'); 
    document.write('GST; $' + hgst + '<br />'); 
    document.write('Total Inc GST; $' + htotal_tax + '<br />'); 
    document.write('hordernumber; ' + hordernumber + '<br />'); 
    } 
+0

好的,謝謝你。這一切似乎都有意義,我只是無法將其放入代碼的上下文中。用我原來的代碼,我在html上引用了這種顯示,現在當我用上面代替第3行和第13行之間的所有內容時,我沒有得到輸出。難道我做錯了什麼?謝謝! – user2397654

+0

嘿,謝謝你的回覆,請原諒我的無能,但我現在無法得到輸出,我應該仍然是使用'var hprice = all [12];''和'document.write('Price of Bags ; $'+ hprice +'
');'輸出數據不應該我? – user2397654

+0

對於一件事你寫出了計算結果,同時仍然在for循環中進行計算。您需要在循環中收集結果,然後在循環外部記錄document.write。爲什麼你在'var logLength = localStorage.length - 1;'中減去1的存儲長度? – anomaaly

相關問題