//显示
function ShowList() {
$.ajax({
url: "/Home/StudentList1",
type: "post",
success: function (data) {
$("#tb").html("");
for (var i = 0; i < data.length; i++)
{
$("#tb").append(
"<tr>"
+ "<td>" + data[i].SID + "</td>"
+ "<td>" + data[i].Sxuehao + "</td>"
+ "<td>" + data[i].SNAME + "</td>"
+ "<td>" + data[i].SAGE + "</td>"
+ "<td><input type="button" value="删除" onclick="deldte("
+ data[i].SID + ")" class="btn btn-primary"/></td>"
+ "<td><input type="button" value="修改"
onclick="update(this)" class="btn btn-primary"/></td>"
+ "</tr>")
}
}
})
}
//修改
function update(id)
{
if ($(id).val() == "修改") {
var Sid = $(id).parent().parent().find("td").eq(0).html();
var Sxuehao = $(id).parent().parent().find("td").eq(1).html();
var Sname = $(id).parent().parent().find("td").eq(2).html();
var Sage = $(id).parent().parent().find("td").eq(3).html();
$(id).parent().siblings().eq(1).html("<input name="Text2" type='text' value='" + Sxuehao + "'>");
$(id).parent().siblings().eq(2).html("<input name="Text3" type='text' value='" + Sname + "'>");
$(id).parent().siblings().eq(3).html("<input name="Text4" type='text' value='" + Sage + "'>");
$(id).val("确定");
}
else
{
var Txt_Sid = $(id).parent().siblings().eq(0).html();
var txt_Sxuehao = $(id).parent().parent().find("input[name='Text2']").val();
var txt_Sname = $(id).parent().parent().find("input[name='Text3']").val();
var txt_Sage = $(id).parent().parent().find("input[name='Text4']").val();
$.ajax({
url: "/Home/update",
type: "post",
datatype:"json",
data: { SID: Txt_Sid, Sxuehao: txt_Sxuehao, SNAME: txt_Sname, SAGE: txt_Sage },
success: function (data)
{
if (data.code == 1)
{
alert(data.msg);
ShowList();
}
else
{
alert("修改失败");
}
}
})
}
}
//删除
function deldte(id)
{
$.ajax({
url: "/Home/del?id=" + id,
type:"post",
success: function (data)
{
if (data>0) {
alert("删除成功!");
ShowList();
}
else
{
alert("删除失败");
}
}
})
}
//添加
$(function ()
{
ShowList();
$("#Butadd").click(function ()
{
$("#tb").append(
"<tr class='addtr'>"
+ "<td> </td>"
+ "<td><input type='Text' value='' /></td>"
+ "<td><input type='Text' value='' /></td>"
+ "<td><input type='Text' value='' /></td>"
+"</tr>"
)
//保存
$("#Butbao").click(function ()
{
var res = "";
$(".addtr").each(function ()
{
var number = $(this).find("input").eq(0).val();
var name = $(this).find("input").eq(1).val();
var age = $(this).find("input").eq(2).val();
var stu_stu = number + "," + name + "," + age + "|";
res += stu_stu;
})
$.ajax({
url: "/Home/Studentadd",
type: "post",
data:{a:res},
success: function (data)
{
if (data.code==1)
{
alert(data.msg);
ShowList();
}
else
{
alert(data.msg);
}
}
})
})
$("#Butdel").click(function ()
{
$.ajax({
url: "/Home/del",
type: "post",
data: { a: res },
success: function (data) {
if (data.code == 1) {
alert(data.msg);
ShowList();
}
else {
alert(data.msg);
}
}
})
})
})
})
</script>
</head>
<body>
<div>
<input id="Butadd" type="button" value="添加" />
<input id="Butbao" type="button" value="保存" />
<table id="table1">
<thead>
<tr>
<td>编号</td>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
</thead>
<tbody id="tb">
</tbody>
</table>
</div>
</body>
</html>