0
我想知道使用哪個代碼創建表單,用戶可以在其中輸入內容並點擊提交(或按鈕),數據將會被存儲在一個數組(或JSON對象)中,然後他們可以再次執行,之前的數據將被「推送」,因此用戶可以繼續添加它。這將繼續如此等等。如果可以使用「本地存儲」將這些信息存儲在用戶機器上,我也很感興趣。使用Javascript中的表單逐個添加輸入到數組或對象
這個問題是一個問題,我在這裏度過的延續: Add form input to array with javascript using unshift()
下面是一些代碼。此代碼做我想要的,但只有一個輸入項目。
謝謝。
<form>
<input id="input" type="text"/>
<button id="button"> Click me </button>
</form>
<script>
var input = document.getElementById("input"); // save the object
var button = document.getElementById("button");
var myArray = [];
button.onclick = function alerted(){
myArray.unshift(input.value); // get the value
alert(myArray);
return false;
};
</script>
在你的例子中只有一個輸入......多輸入有什麼「不起作用」?請更好地解釋你的問題。也可以看一下http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/,解釋爲什麼「JSON對象」不存在。 –
當我提交表單時,表單字段需要空白,並允許我繼續添加存儲在數組中的多個項目。我想我可能只是回答我自己的問題,因爲我需要的只是表單域爲空。我會去看看它。謝謝 – William
您是否在表單提交後重新加載頁面?如果是這樣,所有以前存儲的值將會丟失。例如,如你所說的,'localStorage',你必須在頁面加載之間堅持它們。 –