![](/img/trans.png)
[英]how to get the array from the javascript function to the html form as input field value by body onload() function?
[英]How to store a value from an HTML input field in a javascript array?
我想知道如何在將按鈕單擊到JavaScript數組中后,如何從HTML輸入字段中存儲值,然后如何在屏幕上顯示該數組數據。
謝謝,下面是我的html代碼:
<!DOCTYPE HTML>
<html>
<head>
<title>Checklist</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>
<body>
<input type="text" id="item" placeholder="Enter an item">
<button id="add">Add Item</button>
</body>
</html>
您可以創建一個數組,單擊button
,將input type="text"
元素值推送到數組; 使用application/octet-stream
類型的window.open()
data URI
下載文件。 要更新現有文件,您可以包含<input type="file">
元素,以供用戶上傳已下載的相同文件,將當前input type="text"
值推送到同一文件,然后提示用戶下載具有相同名稱的原始文件。 雖然注意,但是用戶決定下載文件。
另請參見編輯,保存,自修改HTML文檔。 格式生成的HTML,JavaScript
<!DOCTYPE HTML>
<html>
<head>
<title>Checklist</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(function() {
var input = $(":text");
var button = $("button");
var arr = [];
button.click(function() {
arr.push(input.val());
input.val("");
window.open("data:application/octet-stream," + JSON.stringify(arr));
})
})
</script>
</head>
<body>
<input type="text" id="item" name="save" placeholder="Enter an item">
<button id="add">Add Item</button>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.