1、modal 模态框 的代码如下:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<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="exampleModalLabel">New message</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="control-label">Title:</label>
<input type="text" class="form-control" id="titletext">
</div>
<div class="form-group">
<label for="message-text" class="control-label">Body:</label>
<textarea class="form-control" id="bodytext"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" οnclick="edititem(this)">Save</button>
</div>
</div>
</div>
</div>
在模态框中对两个input框加上了id
2、调用模态框的页面
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>title</th>
<th>body</th>
<th>time</th>
<th>operation</th>
</tr>
</thead>
<tbody>
{% for emp in pagination.objs %}
<tr>
<td id="title">{{ emp.title }}</td>
<td id="body">{{ emp.body }}</td>
<td id="time">{{ emp.timestamp }}</td>
<td>
<button type="button" class="btn btn-default btn-xs" οnclick="edititem(this)">
<span class="glyphicon glyphicon-edit" aria-hidden="true"><span>
</button>
<button type="button" class="btn btn-default btn-xs" οnclick="getrmitem(this)">
<span class="glyphicon glyphicon-remove" aria-hidden="true"><span>
</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
效果如下:
当我点击编辑图标时,将这一行的数据传给模态框的input中
3、js实现向模态框modal传递数据
function edititem(obj){
var tr=$(obj).parent().parent();
var title = tr.children("td#title").text();
var body = tr.children("td#body").text();
$('#titletext').val(title);
$('#bodytext').val(body);
$('#exampleModal').modal('show')
};
4、结果如下