如何使用jQuery传递表值以在Bootstrap Modal中形成输入值?
问题描述:
编辑:我终于设法传递值.val(),但是,它只有在我改变输入字段名称和ID是相同的后才起作用。如何使用jQuery传递表值以在Bootstrap Modal中形成输入值?
我不明白如何使用值从表中fullfill我的引导模式窗体输入字段 - 我想创建编辑功能。因此,当按下编辑按钮时,它将打开带有窗体的模态窗口,并且已经从当前项目填充满了字段。
我以前console.log()
所以值传递给JS。我不确定的唯一事情是如何通过项目ID。
但是,我真的没有得到如何满足这些值的输入字段。我试图$('#frm_name').text(name)
- 没有任何反应,形成以空字段中显示, $('#frm_name').append(name)
- 没有任何反应,形成以空字段和$('#frm_name').value(name)
打开 - 编辑按钮都没有工作,没有开放模式。
模态CODE
<div class="modal fade" id="myModal" 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">Edit record</h4>
</div>
<div class="modal-body">
<form id="" action="" method="POST" class="form">
<div class="form-group">
<label for="name1">Name</label>
<input type="text" class="form-control" name="name1" id="frm_name" value="test_value">
</div>
<div class="form-group">
<label for="ean1">EAN</label>
<input type="text" class="form-control" name="ean1" id="frm_ean" value="">
</div>
<div class="form-group">
<label for="price1">Price</label>
<input type="text" class="form-control" name="price1" id="frm_price" value="">
</div>
<div class="form-group">
<label for="date1">Date</label>
<input type="text" class="form-control" name="date1" id="frm_date" value="">
</div>
<input type="hidden" name="id1" id="frm_id" value="">
<input type="submit" name="submit" class="btn btn-primary btn-custom" value="Save changes">
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<div id="results"></div>
</div>
</div>
</div>
表的代码
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<td class="text-left" >ID</td>
<td class="text-left" >Product name</td>
<td class="text-left">EAN</td>
<td class="text-left">Special price</td>
<td class="text-left">Date Added</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
{% if prices %}
{% for price in prices %}
<tr>
<td class="text-left td_id">{{ price.id }}</td>
<td class="text-left td_name">{{ price.product_name }}</td>
<td class="text-left td_ean">{{ price.ean_code }}</td>
<td class="text-left td_price">{{ price.special_price }}</td>
<td class="text-left td_date">{{ price.date_added }}</td>
<td class="text-right">
<button type="button" id="item_{{ price.id }}" data-toggle="tooltip" title="Edit" class="btn btn-primary editModal" data-dismiss="modal"><i class="fa fa-pencil"></i></button>
<a href="index.php?route=customer/customer/deletePrice&user_token={{ token }}&customer_id={{ customer_id }}&customer_price_id={{ price.id }}" data-toggle="tooltip" title="delete" class="btn btn-danger"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td class="text-center" colspan="5">{{ text_no_results }}</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
<div class="row">
<div class="col-sm-6 text-left">{{ pagination }}</div>
<div class="col-sm-6 text-right">{{ results }}</div>
</div>
JS CODE
<script>
$(".editModal").click(function(){
var $row = $(this).closest('tr');
var $name = $row.find('.td_name').text();
console.log($name);
$('#frm_name').text(name);
$('#myModal').modal('show');
});
</script>
答
您可以设置动态内容的模态
<button data-toggle="modal" data-target="#view-modal" data-id="<?php echo $item_id; ?>" id="getUser" class="btn btn-sm btn-info"><i class="glyphicon glyphicon-eye-open"></i> View</button>
<div id="view-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">
<i class="glyphicon glyphicon-user"></i> User Profile
</h4>
</div>
<div class="modal-body">
<div id="modal-loader" style="display: none; text-align: center;">
<img src="ajax-loader.gif">
</div>
<!-- content will be load here -->
<div id="dynamic-content"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
和使用jQuery阿贾克斯根据ITEM_ID
$(document).ready(function(){
$(document).on('click', '#getUser', function(e){
e.preventDefault();
var item_id= $(this).data('id'); // it will get id of clicked row
$('#dynamic-content').html(''); // leave it blank before ajax call
$('#modal-loader').show(); // load ajax loader
$.ajax({
url: 'getitem.php',
type: 'POST',
data: 'id='+item_id,
dataType: 'html'
})
.done(function(data){
console.log(data);
$('#dynamic-content').html('');
$('#dynamic-content').html(data); // load response
$('#modal-loader').hide(); // hide ajax loader
})
.fail(function(){
$('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
$('#modal-loader').hide();
});
});
});
请接受,如果满意给予好评的答案加载模态的模态的身体。 –