[英]Passing data using jquery or javascript in modal bootstrap
I have a table that represented from SQL Table that looping all data with PHP. 我有一个用SQL表表示的表,该表用PHP循环了所有数据。
HTML 的HTML
<tbody>
<?php
$no = 1;
foreach($data_request as $data) {
?>
<tr>
<td class="center"><?php echo $no++.". ";?> </td>
<td class="sorting1"><?php echo $data['id_request'];?> </td>
<td class="center"><?php echo "$nama"; ?></td>
<td class="right">
<a class="btn btn-danger" href="#">
<i class="halflings-icon white trash"></i> Close</a>
<a class="btn btn-success" href="#" id="print"?>">
<i class="halflings-icon pencil"></i> Preview </a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="modal hide fade" id="myModal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Print Preview</h3>
</div>
<div class="modal-body">
/*This would be a preview*/
</div>
<div class="modal-footer">
<a href="#" class="btn btn-primary">Make it to PDF</a>
<a href="#" class="btn" data-dismiss="modal">Close</a>
</div>
</div>
My problem is, I wanna display a row data to modal bootstrap if users clicked the preview button. 我的问题是,如果用户单击预览按钮,我想显示行模态引导数据。 So, if user click the button in row 1, modal would be displaying all field/data in first row of table.
因此,如果用户单击第1行中的按钮,则模式将在表的第一行中显示所有字段/数据。 Untill now, I have success passing data using jquery like this :
到目前为止,我已经成功使用jquery这样传递数据了:
<script>
$('.btn-success').click(function(){
var address = [];
$(this).closest('tr').find('td').not(':last').each(function() {
var textval = $(this).text(); // this will be the text of each <td>
address.push(textval);
});
alert(address.join('\n'));
});
</script>
I am confusing to pass the data from script to modal, anyone can help ? 我很困惑将数据从脚本传递到模式,有人可以帮忙吗?
Better to give unique ids to the rows while rendering it from SQL DB. 从SQL DB渲染行时最好给行赋予唯一的ID。
<tbody>
<?php
$no = 1;
foreach($data_request as $data) {
?>
<tr id="row_<?php echo $data['id_request']; ?>">
<td class="center"><?php echo $no++.". ";?> </td>
<td class="sorting1"><?php echo $data['id_request'];?> </td>
<td class="center"><?php echo "$nama"; ?></td>
<td class="right">
<a class="btn btn-danger" href="#">
<i class="halflings-icon white trash"></i> Close</a>
<a class="btn btn-success" href="#" id="print_<?php echo $data['id_request']; ?>"">
<i class="halflings-icon pencil"></i> Preview </a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="modal hide fade" id="myModal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Print Preview</h3>
</div>
<div class="modal-body">
/*This would be a preview*/
</div>
<div class="modal-footer">
<a href="#" class="btn btn-primary">Make it to PDF</a>
<a href="#" class="btn" data-dismiss="modal">Close</a>
</div>
</div>
Script: 脚本:
<script>
$('.btn-success').click(function(){
var address = [];
var trId = $(this).attr('id').split('_')[1];
$('#row_'+trId).find('td').each (function() {
address.push($(this).text());
});
alert(address.join('\n'));
$('#myModal > .modal-body').html(address.join('\n'));
$('#myModal').modal('show');
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.