我有一组要以表格格式显示的JSON对象,并且输入字段很少。在表单字段中输入的值应在Json对象中更新,并应显示在现有表中。以下是代码。我无法更新表中的JSON对象。

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">
        </script>

        </head>
    <body>
        <div id="wrap">
        <header>
            <h1>Task Tracker</h1>
            <h4>v2.0</h4>
        </header>
        <div id="main">
            <div id="left-side">
                <h3>Create a Task</h3>
                <form action="" method="post">
                    <div id="taskname" class="task-details">
                    <label>Name</label><br><input name="name" type="text">
                </div>
                <div id="taskdate" class="task-details">
                    <label>Date</label><br><input name="date" type="text">
                </div>
                <div id="assigned" class="task-details">
                    <label>Assigned To</label><br><input name="assigne" type="text">
                </div>
                    <input type="submit" value="Submit">
                </form>
            </div>
            <div id="right-side">
                <h3>Existing Tasks</h3>
                <div id="div1">
                </div>
            </div>
        </div>
    </div>
        <script>

$.fn.serializeObject = function()
        {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
            };

            $(function() {
                $('form').submit(function() {
                var data= console.log(JSON.stringify($('form').serializeObject()));
                return false;
                obj.push("data");
            });
                var obj=[

    {"name": "Task Name", "date": "12/01/2012", "assigned": "John Doe" },
    {"name": "Task Name", "date": "12/02/2012", "assigned": "John Doe" },
    {"name": "Task Name", "date": "12/03/2012", "assigned": "John Doe" },
    {"name": "Task Name", "date": "12/04/2012", "assigned": "John Doe" },
    {"name": "Task Name", "date": "12/05/2012", "assigned": "John Doe" },
    {"name": "Task Name", "date": "12/06/2012", "assigned": "John Doe" },
    {"name": "Task Name", "date": "12/07/2012", "assigned": "John Doe" }

]


var tbl=$("<table/>").attr("id","mytable");
$("#div1").append(tbl);
for(var i=0;i<obj.length;i++)
{
    var tr="<tr>";
    var td1="<td>"+obj[i]["name"]+"</td>";
    var td2="<td>"+obj[i]["date"]+"</td>";
    var td3="<td>"+obj[i]["assigned"]+"</td></tr>";

   $("#mytable").append(tr+td1+td2+td3);
}
            });



        </script>

    </body>
</html>

最佳答案

使用e.preventDefault();阻止表单提交操作。
  
  不要stringifyserializeObject,否则它将在push数组中的obj字符串
  
  在tr操作上在表中附加submit


尝试这个:



$.fn.serializeObject = function() {
  var o = {};
  var a = this.serializeArray();
  $.each(a, function() {
    if (o[this.name] !== undefined) {
      if (!o[this.name].push) {
        o[this.name] = [o[this.name]];
      }
      o[this.name].push(this.value || '');
    } else {
      o[this.name] = this.value || '';
    }
  });
  return o;
};

$(function() {
  $('form').submit(function(e) {
    e.preventDefault();
    var data = $('form').serializeObject();
    var tr = "<tr>";
    tr += "<td>" + data["name"] + "</td>";
    tr += "<td>" + data["date"] + "</td>";
    tr += "<td>" + data["assigned"] + "</td></tr>";
    $("#mytable").append(tr);
    obj.push(data);
    this.reset();
  });
  var obj = [{
    "name": "Task Name",
    "date": "12/01/2012",
    "assigned": "John Doe"
  }, {
    "name": "Task Name",
    "date": "12/02/2012",
    "assigned": "John Doe"
  }, {
    "name": "Task Name",
    "date": "12/03/2012",
    "assigned": "John Doe"
  }, {
    "name": "Task Name",
    "date": "12/04/2012",
    "assigned": "John Doe"
  }, {
    "name": "Task Name",
    "date": "12/05/2012",
    "assigned": "John Doe"
  }, {
    "name": "Task Name",
    "date": "12/06/2012",
    "assigned": "John Doe"
  }, {
    "name": "Task Name",
    "date": "12/07/2012",
    "assigned": "John Doe"
  }];
  var tbl = $("<table/>").attr("id", "mytable");
  $("#div1").append(tbl);
  var html = '';
  for (var i = 0; i < obj.length; i++) {
    html += "<tr>";
    html += "<td>" + obj[i]["name"] + "</td>";
    html += "<td>" + obj[i]["date"] + "</td>";
    html += "<td>" + obj[i]["assigned"] + "</td></tr>";
  }
  $("#mytable").append(html);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="wrap">
  <header>
    <h1>Task Tracker</h1>
    <h4>v2.0</h4>
  </header>
  <div id="main">
    <div id="left-side">
      <h3>Create a Task</h3>
      <form action="" method="post">
        <div id="taskname" class="task-details">
          <label>Name</label>
          <br>
          <input name="name" type="text">
        </div>
        <div id="taskdate" class="task-details">
          <label>Date</label>
          <br>
          <input name="date" type="date" type="text">
        </div>
        <div id="assigned" class="task-details">
          <label>Assigned To</label>
          <br>
          <input name="assigned" type="text">
        </div>
        <input type="submit" value="Submit">
      </form>
    </div>
    <div id="right-side">
      <h3>Existing Tasks</h3>
      <div id="div1">
      </div>
    </div>
  </div>
</div>





Fiddle here

关于javascript - 如何使用javascript或jquery以表格式显示JSON对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35424495/

10-12 16:21