代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="./css/bootstrap-table.css" rel="stylesheet">
<script src="./js/jquery-3.4.1.min.js"></script>
<script src="./js/bootstrap.min.js"></script>
<title>Title</title>
<script>
$(document).ready(function () {
var x=" ";
var users = [
{ "id":"1","name":"roob" ,"password":"123" ,"isAdmin":"1"},
{ "id":"2","name":"goog" , "password":"456" ,"isAdmin":"0"},
];
for (i in users) {
x="<tr><td>" +"<input type='checkbox'/>"+ "</td>"+"<td>"+users[i].id+"</td>"+"<td>" +users[i].name+ "</td>"+"<td>" +users[i].password + "</td>"+"<td>" +users[i].isAdmin + "</td></tr>";
$("#tb").append(x);
}
})
//发送一个 HTTP POST 请求页面并获取返回内容
// $(document).ready(function(){
// $("#button1").click(function(){
// $.post("/try/ajax/demo_test_post.php",{
// name:"菜鸟教程",
// url:"http://www.runoob.com"
// },
// function(data,status){
// alert("数据: \n" + data + "\n状态: " + status);
// });
// });
// });
//保存用户信息
function saveUser() {
$.ajax({
url: "http//:wwww.baidu.com",//获取数据的URL
data:$(".fm").serialize(),//前端将数据给后端
type: "POST",//HTTP请求方法
dataType : 'JSON',//获取数据执行方式
success:function(data){
if(data.status == 'True'){//传入为JSON对象格式
alert('连接成功');
}
else{
$("#labletip").show();
}
},
error:function(err){
alert('连接失败');
}
});
}
function editUser(){//data-toggle="modal" data-target="#myModal-1" //模态框
var vals = [];
// var x=0;//记录选中的数据下标值,从0开始
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
}
);
if(vals.length<1||vals.length>=2) {
$("#editButton").removeAttr("data-target");
//$("editButton").removeAttr("data-target");
alert("请选择一条数据");
} else {
//回显数据
var userArr=new Array();
var i=0;
$('input:checkbox:checked').parent().siblings().each(function (index) {
//userArr[i++].push($(this).val());
userArr[i++]=$(this).text()
//alert($(this).text());
})//id="username-1"
// $("#myModal-1").find("input").each(function () {
// if($(this)[i])
// })
//var y=userArr[1];
$("input[id='username-1']").val(userArr[1]);
$("input[id='pwd-1']").val(userArr[2]);
$("input[id='addre-1']").val(userArr[3]);
//数据显示在模态框
$("#editButton").attr("data-target","#myModal-1");
}
};
//删除用户
function deleteUser() {
var vals = [];
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
}
);
if(vals.length<1) {
//$("#editButton").removeAttr("data-target");
//$("editButton").removeAttr("data-target");
alert("请至少选择一条数据");
}
else{
var userArr=new Array();
var i=0;
$('input:checkbox:checked').parent().siblings().each(function (index) {
//userArr[i++].push($(this).val());
userArr[i++]=$(this).text()
//alert($(this).text());
})//id="username-1"
// $("#myModal-1").find("input").each(function () {
// if($(this)[i])
// })
//var y=userArr[1];
alert("要删除数据的id为:"+userArr[0]);
}
}
</script>
</head>
<body>
<!--工具栏-->
<div class="toolbar" style="height: 100px">
<ul class="nav nav-pills">
<li role="presentation" class="active" >
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" ><i class="glyphicon glyphicon-plus-sign"></i>
新增
</button>
</li>
<li role="presentation"class="active">
<button id="editButton" onclick="editUser()" type="button" class="btn btn-primary btn-lg" data-toggle="modal"><i class="glyphicon glyphicon-edit"></i>
修改
</button>
</li>
<li role="presentation"class="active">
<button id="deleteButton" onclick="deleteUser()" type="button" class="btn btn-primary btn-lg" data-toggle="modal"><i class="glyphicon glyphicon-remove"></i>
删除
</button>
</li>
<li role="presentation" class="active dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false" style="width:110px;height: 46px ">
操作设置<span class="caret"></span>
</a>
<ul class="dropdown-menu">
...
</ul>
</li>
</ul>
</div>
<!--显示表格-->
<div>
<table class="table table-striped" id="tb">
<tr>
<th>选中</th><th>id</th><th>用户名</th><th>密码</th><th>是否管理员</th>
</tr>
</table>
</div>
<!-- Modal 添加模态框-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h2 class="modal-title" id="myModalLabel">新增用户信息</h2>
</div>
<div class="modal-body">
<form class="fm">
<table class="table table-hover table-bordered">
<tr>
<th>用户名</th>
<td><input type="text" name="username" class="form-control" id="username" /></td>
</tr>
<tr>
<th>密码</th>
<td><input type="password" name="password" class="form-control" id="pwd" /></td>
</tr>
<tr>
<th>是否管理员</th>
<td><input type="text" name="idAdmin" class="form-control" id="addre" /></td>
</tr>
</table>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="button1" class="btn btn-primary" onclick="saveUser()" >Save changes</button>
</div>
</div>
</div>
</div>
<!-- Modal 编辑模态框-->
<div class="modal fade" id="myModal-1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel-1">编辑用户信息</h4>
</div>
<div class="modal-body">
<form class="fm">
<table class="table table-hover table-bordered">
<tr>
<th>用户名</th>
<td><input type="text" name="username" class="form-control" id="username-1" /></td>
</tr>
<tr>
<th>密码</th>
<td><input type="password" name="password" class="form-control" id="pwd-1" /></td>
</tr>
<tr>
<th>是否管理员</th>
<td><input type="text" name="idAdmin" class="form-control" id="addre-1" /></td>
</tr>
</table>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="button1-1" class="btn btn-primary" onclick="saveUser()" >Save changes</button>
</div>
</div>
</div>
</div>
</body>
</html>