javascript - 将输入保存在数组中并显示所有项目 jquery\javascript

标签 javascript arrays

我是 jQuery 的初学者。我希望我的表单的所有输入值都在数组中 =(项目 01,项目 02 和)保存 ..

它应该在表格中显示项目吗?

我做了什么但它不起作用:

<form id="form_id">
    <label for="firstname">First Name:</label>
    <input type="text" id="firstname" name="name" value="" placeholder="Max" required="required" autofocus="autofocus" />
    <label for="lastname">Last Name:</label>
    <input type="text" id="lastname" name="name" value="" placeholder="Max" required="required" autofocus="autofocus" />
    <input type="submit" value="submit" id="submit-button" />
</form>

<div id="table">
<h2>List of all person</h2>
<table class="footable">
    <thead>
        <tr>
            <th data-class="expand"> First Name </th>
            <th> Last Name </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>e.g --> array[1].firstname</td>
            <td>e.g --> array[1].lastname</td>
        </tr>
    </tbody>
</table>
</div>

Javascript 代码:

  $(function() {
    $('#submit-button').click(function() {
        var formInputs = new Array();
        //Get the form ID
        var id = $(this).parent().attr('id');
        $('#' + id + ' input').each(function() {
            //Get the input value
            var inputValue = $(this).val();
            //Get the input id
            var inputId = $(this).attr('id');
            //Add them to the array
            formInputs[inputId] = inputValue;
        });

        show....
    });
});

最佳答案

试试这个

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 <script type="text/javascript">
  $(document).ready(function(){
    $("#submit-button").click(function(){
      var data = $('#form_id').serializeArray();
      var obj = {};
      for (var i = 0, l = data.length; i < l; i++) {
          obj[data[i].name] = data[i].value;
      }
      $('table.footable tbody').append('<tr><td>'+obj['firstname']+'</td><td>'+obj['lastname']+'</td></tr>');
      $("#firstname").val('');
      $("#lastname").val('');
    })
  })
 </script>
<form id="form_id">
    <label for="firstname">First Name:</label>
    <input type="text" id="firstname" name="firstname" value="" placeholder="Max" required="required" autofocus="autofocus" />
    <label for="lastname">Last Name:</label>
    <input type="text" id="lastname" name="lastname" value="" placeholder="Max" required="required" autofocus="autofocus" />
    <input type="button" value="submit" id="submit-button" />
</form>

<div id="table">
<h2>List of all person</h2>
<table class="footable">
    <thead>
        <tr>
            <th data-class="expand"> First Name </th>
            <th> Last Name </th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
</div>

关于javascript - 将输入保存在数组中并显示所有项目 jquery\javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16083063/

相关文章:

javascript - 使用 Firebase 在特定用户 UID 下保存数据

javascript - Controller 之间的 Angular 共享数据 : service vs event

javascript - promise 中的 promise ?

angularjs - 使用 ng-options 和数组作为值进行选择

javascript - 根据另一个数组中的条目拆分数组

javascript - 使用 jQuery 和 CSS 创建条形音箱

javascript - element.setAttribute() 方法如何让它在 I.E 中工作?

c# - 如何裁剪二维数组的分段?

javascript - 获取可变深度数组中的第一个数字数组

c++ - 如何在 C++ 中获取二维动态字符串数组的维度?