jquery - 使用引导模式框更改输入数据
问题描述
我使用 jquery 将动态表单数据输入加载到引导模式框:
html:
<table width="100%" class="table table-striped table-bordered table-hover">
<thead>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
<th>Action</th>
</thead>
<tbody>
<tr>
<td><input type="text" id="first1" value="name1"></td>
<td><input type="text" id="last1" value="last1"></td>
<td><button type="button" class="btn btn-success edit" value="1"> Edit</button></td>
</tr>
<tr>
<td><input type="text" id="first2" value="name2"></td>
<td><input type="text" id="last2" value="last2"></td>
<td><button type="button" class="btn btn-success edit" value="2"> Edit</button></td>
</tr>
</tbody>
</table>
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<center>
<h4 class="modal-title" id="myModalLabel">Edit User</h4>
</center>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="form-group input-group">
<span class="input-group-addon" style="width:150px;">Firstname:</span>
<input type="text" style="width:350px;" class="form-control" id="efirst">
</div>
<div class="form-group input-group">
<span class="input-group-addon" style="width:150px;">Lastname:</span>
<input type="text" style="width:350px;" class="form-control" id="elast">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"> Cancel</button>
<button type="button" class="btn btn-success"> Update</button>
</div>
</div>
</div>
</div>
JS:
$(document).ready(function() {
$(document).on('click', '.edit', function() {
var id = $(this).val();
var first = $('#first' + id).val();
var last = $('#last' + id).val();
$('#edit').modal('show');
$('#efirst').val(first);
$('#elast').val(last);
});
});
在第一步中此代码工作正常并将数据加载到引导模式中,但在第二步中,我如何在单击更新按钮并设置为原始数据后使用模式编辑/更改输入数据。
解决方案
像这样?
JSFIDDLE
https://jsfiddle.net/m79vrtsx/
$(document).ready(function() {
$(document).on('click', '.edit', function() {
var id = $(this).val();
var first = $('#first' + id).val();
var last = $('#last' + id).val();
$('#edit').modal('show');
$('#efirst').val(first);
$('#elast').val(last);
$('.submit_btn').val(id);
});
$(document).on('click', '.submit_btn', function() {
var id = $(this).val();
var first = $('#efirst').val();
var last = $('#elast').val();
$('#first' + id).val(first);
$('#last' + id).val(last);
$('#edit').modal('hide');
});
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<table width="100%" class="table table-striped table-bordered table-hover">
<thead>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
<th>Action</th>
</thead>
<tbody>
<tr>
<td><input type="text" id="first1" value="name1"></td>
<td><input type="text" id="last1" value="last1"></td>
<td><button type="button" class="btn btn-success edit" value="1"> Edit</button></td>
</tr>
<tr>
<td><input type="text" id="first2" value="name2"></td>
<td><input type="text" id="last2" value="last2"></td>
<td><button type="button" class="btn btn-success edit" value="2"> Edit</button></td>
</tr>
</tbody>
</table>
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-id="1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<center>
<h4 class="modal-title" id="myModalLabel">Edit User</h4>
</center>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="form-group input-group">
<span class="input-group-addon" style="width:150px;">Firstname:</span>
<input type="text" style="width:350px;" class="form-control" id="efirst">
</div>
<div class="form-group input-group">
<span class="input-group-addon" style="width:150px;">Lastname:</span>
<input type="text" style="width:350px;" class="form-control" id="elast">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
<button type="button" class="btn btn-success submit_btn" value=""> Update</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
推荐阅读
- android - 使APP android读取文件的内容 - IONIC
- node.js - 如何使用 Winston 库在 Node.js 中每天将 logrotate 日志记录到单独的文件中
- c++ - 为什么调用模板类函数时会出现 SEGFAULT?
- python - 想要通过占位符选择用于运行时训练的变量
- python-3.x - 将类及其功能从一个文件导入到另一个文件
- operating-system - Android One 是否支持 ARCore?
- laravel - Laravel whereRaw - 如何逃避'?
- vuejs2 - 在异步方法函数中调用方法函数
- html - 如何在不干扰css的情况下添加链接
- laravel-5 - Laravel 5.8 获取重复数据列表