我正在尝试获取按钮所属的表行的id,并想出在<span>
元素内打印行的id(与数据库中的ID相同)一个模态框。它工作得很好但是当我尝试使用DOM获取<span>
标签中的文本时,它是空的。我尝试使用AJAX发布数据,但我似乎无法让它工作,所以我采用了这个。
这是javascript / jQuery
$('#ViewModal').on('show.bs.modal', function(e) { //Fires when modal is opened
var $modal = $(this),
inventory_id = e.relatedTarget.id;
$.ajax({
cache: false,
type: 'POST',
url: 'view_items.php',
data: {inventory_id: trId},
success: function(data) {
$modal.find('.inventory_id').html(trId);
}
});
})
这是我的HTML和PHP:
<div id="ViewModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="ItemView" 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>
<h4 class="modal-title" id="ItemView">Item view</h4>
</div>
<div class="modal-body">
<span id=id class='inventory_id hidden'></span>
<?php
//query here to get the record based on the text in the element
$doc = new DOMDocument();
$id = $doc->getElementById('id');
$inventory_id = $id;
$echo $inventory_id //returns an empty value
//bind to parameters
$result = $query->get_result();
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
//I'm going to echo result here
}
?>
</div>
我做对了吗?或者我应该使用另一种方法将变量值传递给PHP?