[英]Cannot populate record to bootstrap modal when a link is clicked
请我需要一些帮助,我一直在努力地工作,以便在单击链接时填充记录以引导到模态,以便用户可以编辑和更新记录。 链接不是表格形式,而是表格形式。 以下是我的无效代码:
HTML
<div class="widget-content">
<form action="#" method="GET">
<table class="table table-striped table-bordered table-hover table-checkable datatable">
<thead>
<tr>
<th class="checkbox-column">
<input type="checkbox" class="uniform">
</th>
<th>Actions</th>
<th>Code</th>
<th>Full Name</th>
<th>Company Name</th>
<th>Business Location</th>
<th>Telephone</th>
<th>Credit Limit</th>
</tr>
</thead>
<tbody>
<?php
$query="select * from customer_vendor";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC) ){
$firstname = $row['first_name'];
$lastname = $row['last_name'];
$fullname = $firstname.' '.$lastname;
$climit = number_format($row['credit_limit'],2);
echo
"<tr>
<td class=\"checkbox-column\">
<input type=\"checkbox\" class=\"uniform\" >
<input type=\"hidden\" >
</td>
<td>
<ul class=\"table-controls\">
<li><a class=\"bs-tooltip\" id=\"updateit\" name=\"update_cv\" data-class=\"{$row['customer_vendor_id']}\" data-toggle=\"modal\" href=\"#update_customer_vendor\" title=\"Edit\"><i class=\"icon-pencil\"></i></a> </li>
<li><a class=\"bs-tooltip\" data-toggle=\"modal\" href=\"#delete_customer_vendor\" title=\"Delete\"><i class=\"icon-trash\"></i></a> </li>
</ul>
</td>
<td>{$row['customer_vendor_id']}</td>
<td>$fullname</td>
<td>{$row['company_name']}</td>
<td>{$row['business_location']}</td>
<td>{$row['telephone']}</td>
<td>$climit</td>
</tr>";
}
?>
</tbody>
</table>
</form>
<a href="create_new_cust_vend.php"><button class="btn btn-primary"><i class="fa fa-plus"> New</i></button></a>
</div>
模态:
<div class="modal modal-wide fade" id="update_customer_vendor">
<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">Updating customer - Vendor</h4>
</div>
<form action="#" method="POST">
<div class="modal-body" id="update_modal">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" ><i class="fa fa-save"> Update</i></button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
剧本:
<script type="text/javascript">
$(document).ready(function(){
$("#updateit").click(function(){
var studentClass =$("#updateit").val();
$.ajax({
type:"GET",
url:"update_cust_vend.php",
data:"updateit="+studentClass,
success:function(data){
$("#update_modal").html(data);
}
});
});
});
</script>
首先,将以下内容添加到您的click事件中,以防止超链接的默认行为:
$("#updateit").click(function(event){
event.preventDefault();
});
之后,我很困惑,您将在此处使用超链接的值:
var studentClass =$("#updateit").val();
这不是错误吗? 因为您的超链接没有价值...
您可以将值存储在定制属性中。 为此,您必须使用data-
前缀定义属性。 因此,例如data-value
。
因此,您的超级链接将是:
<li><a class=\"bs-tooltip\" id=\"updateit\" ... data-value="1"><i class=\"icon-pencil\"></i></a></li>
要获得价值,请使用:
var studentClass = $(this).attr('data-value');
$(this)
是指您单击的超链接。
在填充html之后的成功处理程序中,您没有显示模式,请添加以下内容:
$('#update_customer_vendor').modal('show');
尝试以下。 我认为应该修复它。
<li><a class=\"bs-tooltip\" id=\"updateit\" name=\"update_cv\" data-class="studentclass" data-toggle=\"modal\" href=\"#update_customer_vendor\" title=\"Edit\"><i class=\"icon-pencil\"></i></a> </li>
链接必须在非输入字段的某个地方包含studentClass,因此不能使用val()。 即上面链接中的data-class =“ studentclass”。
$(document).ready(function(){
$("#updateit").on('click', function(e){
e.preventDefault();
var studentClass =$(this).data('class'); // get studentclass form link.
$.ajax({
type:"GET",
url:"update_cust_vend.php",
data:"updateit="+studentClass,
success:function(data){
$("#update_modal").html(data);
$("#update_customer_vendor").modal();
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.