简体   繁体   English

如何在javascript数组中存储HTML输入字段中的值?

[英]How to store a value from an HTML input field in a javascript array?

I would like to know how to store a value from an HTML input field after a button is clicked into a JavaScript array and then how to display that array data to the screen. 我想知道如何在将按钮单击到JavaScript数组中后,如何从HTML输入字段中存储值,然后如何在屏幕上显示该数组数据。

Thank you, below is my html code : 谢谢,下面是我的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>

You can create an array, at click of button , push input type="text" element value to array; 您可以创建一个数组,单击button ,将input type="text"元素值推送到数组; use window.open() data URI of type application/octet-stream to download file. 使用application/octet-stream类型的window.open() data URI下载文件。 To update existing file, you can include <input type="file"> element for user to upload same file which was downloaded, push current input type="text" value to same file, then prompt user to download original file with same name. 要更新现有文件,您可以包含<input type="file">元素,以供用户上传已下载的相同文件,将当前input type="text"值推送到同一文件,然后提示用户下载具有相同名称的原始文件。 Though note, it is user decision to download file. 虽然注意,但是用户决定下载文件。

See also Edit, save, self-modifying HTML document; 另请参见编辑,保存,自修改HTML文档。 format generated HTML, JavaScript 格式生成的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>

plnkr http://plnkr.co/edit/cQZznvyltX46UO1Y0lDG?p=preview plnkr http://plnkr.co/edit/cQZznvyltX46UO1Y0lDG?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过主体 onload() 函数将数组从 javascript 函数获取到 html 表单作为输入字段值? - how to get the array from the javascript function to the html form as input field value by body onload() function? 如何通过javascript函数设置html输入字段的值 - how to set the value of an html input field from a javascript function 输入字段到数组的值 - Javascript,localstorage - Value from input field into array - Javascript, localstorage 使用jquery或javascript如何将输入字段值存储在数组中,并在按下空格键时再次显示它 - With jquery or javascript how to store a input field value in an array and again display it when the space key pressed 将HTML元素绑定到Javascript对象并将对象存储在数组中 - 使用来自子元素的输入值更新对象 - Bind HTML element to Javascript object and store object in array - Update object with input value from child element 如何将javascript变量值存储到html输入值? - How can I store javascript variable value into html input value? 如何从输入字段中获取值并存储在本地存储中并在javascript中显示到表中 - how to get value from input field and store in local storage and show into table in javascript 如何将PHP中的输入字段值存储到数据库中,然后将其分配给javascript中的变量 - How to store the input field value in PHP into database and then assign it to variable in javascript 如何将输入字段中的字符串值存储在一个空数组中? - How do I store the string value from input field in an empty array? 如何从javascript中的输入字段获取值? - How to get value from an input field in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM