我有一组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>
答案 0 :(得分:1)
使用
e.preventDefault();
来阻止表单提交操作。不要
stringify
到serializeObject
否则会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>
答案 1 :(得分:0)
只需更新您的提交处理程序,并更新名称字段&#34;已分配&#34;在您的HTML中
$('form').submit(function(e) {
e.preventDefault();
var data= $('form').serializeObject();
$("#mytable").append("<tr><td>"+data["name"]+"</td><td>"+data["date"]+"</td><td>"+data["assigned"]+"</td></tr>");
obj.push("data");
});