javascript - 将查询结果的值传递给Modal

标签 javascript php jquery html bootstrap-modal

我想将查询结果传递给Modal。结果包含项目列表和描述 结果的每一行都有一个出价按钮。出价按钮打开包含项目信息的模式。

我想显示模型中相应行的详细信息,但我无法做到。

这是代码。

<div class="col-lg-12">
<center>
 <table class="auction_det" style="width:100%;" border="1">
 <th>ID</th>
    <th>Item Name</th>
    <th>Description</th>
    <th>Cost</th>
    <th>Current Bid</th>
    <th>Close Date</th>
    <th>Image</th>
  <?php
    if( !empty($results) ) 
    {

    foreach($results as $row) {

    echo '<tr>';
    echo '<td id="id">'.$row->item_id.'</td>';
    echo '<td class="name">'.$row->name.'</td>';
    echo '<td class="desc">'.$row->item_desc.'</td>';
    echo '<td class="cost">'.$row->item_cost.'</td>';
    echo '<td class="open">'.$row->open_date.'</td>';
    echo '<td class="date">'.$row->close_date.'</td>';
    echo '<td class="img"><center><img style="width:300px;height:100px;"src='.$row->img_path.'></center></td>';
    ?>
    <td><button type="button" class="btn" data-toggle="modal" data-target="#moreInfo">Bid</button></td><?php    
    echo '</tr>';

}
}
?>
</table>

脚本

<script>
    $(document).ready(){
        $('.btn').click(function(){
            var $row = $(this).closest('tr');
            var name =  $row.find('.name').val();
            $('#name').text(name);
            $('#moreInfo').modal('show');
        });
    });//END document.ready
</script>

模态

 <div class="modal fade" tab-index="-1" id="moreInfo" role="dialog" aria-labelledby="moreInfoLabel" aria-hidden="true">
       <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
            <h3>Item Details</h3>
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="moreInfoLabel"></h4>
           </div>
          <div="modal-body">
        <p>Name:<label id="name"> *QUERY RESULT HERE* </label></p>
</div>

最佳答案

不要使用 var name = $row.find('.name').val(); 使用:

var name = $row.find('.name').text();var name = $row.find('.name').html();

关于javascript - 将查询结果的值传递给Modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39974436/

相关文章:

javascript - 我在对话框表单上的输入值未更新

javascript - AngularJS/Flash(如何在 angular html partial 中嵌入 flash 视频)

php - 登录系统的 cookie 中应该存储什么?

php - Yii CDBCriteria 复杂连接查询

javascript - 如何防止 jQuery 在隐藏元素之前加载修改后的内容?

JQuery 不工作 - Rails

javascript - 获取 fetch 之外的变量

javascript - Vite磁盘缓存问题

php - SQL 查询 - 获取行退出其他表 laravel 上的一些值

jquery - 如何将嵌套的 div 向上移动两个 div 级别以匹配父级?