bootstrap编辑回显案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap Table Edit</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.3/dist/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<table id="data-table" class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr data-id="1" data-name="John" data-email="john@example.com">
<td>1</td>
<td>John</td>
<td>john@example.com</td>
<td><button class="edit-btn btn btn-primary">Edit</button></td>
</tr>
</tbody>
</table>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Edit User</h4>
</div>
<div class="modal-body">
<form id="formData">
<div class="form-group">
<label for="editId">ID</label>
<input type="text" class="form-control" id="editId" readonly>
</div>
<div class="form-group">
<label for="editName">Name</label>
<input type="text" class="form-control" id="editName">
</div>
<div class="form-group">
<label for="editEmail">Email</label>
<input type="text" class="form-control" id="editEmail">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="saveBtn">Save</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#data-table').on('click', '.edit-btn', function () {
var $row = $(this).closest('tr');
var rowData = {
id: $row.data('id'),
name: $row.data('name'),
email: $row.data('email')
};
$('#editId').val(rowData.id);
$('#editName').val(rowData.name);
$('#editEmail').val(rowData.email);
$('#myModal').modal('show');
});
$('#saveBtn').on('click', function () {
var formData = {};
$('#formData input').each(function () {
formData[$(this).attr('name')] = $(this).val();
});
console.log(formData);
$('#myModal').modal('hide');
});
});
</script>
</body>
</html>