jquery从主窗口传递值到弹出窗口

时间:2017-06-26 06:35:29

标签: javascript php jquery web

我想将值从我的主窗口传递到弹出窗口我试着使用jquery 我已经使用项目列表将它们显示为网格视图我想当用户点击详细信息按钮时显示给他弹出窗口我想将价格值传递给弹出窗口模态体中的任何商品

  while ( $sql=mysqli_fetch_assoc($posts)){
        echo' <div class="item  col-xs-6 col-lg-4">
        <div class="thumbnail" >
            <img class="group lis t-group-image" style="height: 221px;
width: 353px; padding: 2px;
" src="../1'.$sql['img'].'" />
            <div class="caption" style="height: 105px;">
                <h4 class="group inner list-group-item-heading">
                    '.$sql['name'].'</h4>
                <p class="group inner list-group-item-text">
                    '.$sql['details'].' </p>
                <div class="row">
                    <div class="col-xs-12 col-md-12">
                        <p id="price1" class="lead">سعر المنتج:'.$sql['price'].'</p></div>

                </div>
            </div>

           <button id="btn1">Show Text</button>
           <button data-toggle="modal" data-id="price1"  id="ww" class="btn btn-primary">details </button>
           <!--button data-toggle="modal" href="#shortModal"  id="ww" class="btn btn-primary">مشاهدة التفاصيل</button-->


            <!--button type="button" onclick="on_create_form_clicked();" class="btn btn-primary" data-toggle="modal" data-target="#product_view"><i class="fa fa-search"></i> Quick View</button -->
        </div>
    </div>';
    }
    ?>

这是弹出模式

<div id="shortModal" class="modal modal-wide fade">
<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">Modal title</h4>
        </div>
        <div class="modal-body">
            <p>One fine body…</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

我想将值传递给弹出窗口以显示详细信息,以便有任何帮助吗?

3 个答案:

答案 0 :(得分:0)

您可以通过使用Jquery来实现这一点 将Id添加到Modal的标题或您希望显示值的位置

<h4 class="modal-title" id ="ans">Modal title</h4>

为要传递给模态的数据添加data-id

<button data-toggle="modal" data-id="yourdata"  id="ww" class="btn btn-primary">details </button>

Javascript:

$("#ww").click(function () {
        $('#ans').val($(this).data('id'));
        $('#shortModal').modal('show');
    });

答案 1 :(得分:0)

您可以使用点击事件并使用$("#price1").text()

获取价格的值
$('#ww').click(function() {
  $("#shortModal .modal-body").text($("#price1").text())
})

$('#ww').click(function() {
  $("#shortModal .modal-body").text($("#price1").text())
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="thumbnail">
  <img class="group lis t-group-image" style="height: 221px;
width: 353px; padding: 2px;
" src="../1'.$sql['img'].'" />
  <div class="caption" style="height: 105px;">
    <h4 class="group inner list-group-item-heading">
      '.$sql['name'].'</h4>
    <p class="group inner list-group-item-text">
      '.$sql['details'].' </p>
    <div class="row">
      <div class="col-xs-12 col-md-12">
        <p id="price1" class="lead">prdoudct price : 2000</p>
      </div>

    </div>
  </div>

  <button id="btn1">Show Text</button>
  <button data-toggle="modal" href="#shortModal" id="ww" class="btn btn-primary">details </button>



</div>

<div id="shortModal" class="modal modal-wide fade">
  <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">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body…</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->

答案 2 :(得分:0)

根据我的理解,您可以在循环中添加modal并设置动态模态 标识。

尝试以下代码。

<强> HTML / PHP

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<?php
$array=array();
$array[0]['name'] = 'Sony Pendrive';
$array[0]['details'] = '16 GB 2.0';
$array[0]['price'] = '120';
$array[1]['name'] = 'PNY Pendrive';
$array[1]['details'] = '32 GB 2.0';
$array[1]['price'] = '220';
foreach($array as $k=>$values){
?>
    <div class="thumbnail" >
        <div class="caption" style="height: 105px;">
            <h4 class="group inner list-group-item-heading">
                <?php echo $values['name']; ?>
            </h4>
            <p class="group inner list-group-item-text">
                <?php echo $values['details']; ?>
            </p>
            <div class="row">
                <div class="col-xs-12 col-md-12">
                <p id="price1" class="lead">prdoudct price : <?php echo $values['price']; ?></p></div>
            </div>
        </div>

       <button id="btn1">Show Text</button>
       <button data-toggle="modal" href="#shortModal_<?php echo $k; ?>"  id="ww" class="btn btn-primary">details </button>
    </div>

    <div id="shortModal_<?php echo $k; ?>" class="modal modal-wide fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                    <h4 class="modal-title">Modal title</h4>
                </div>
                <div class="modal-body">
                    <p>
                        Name : <?php echo $values['name']; ?> <br />
                        Details : <?php echo $values['details']; ?> <br />
                        Price : <?php echo $values['price']; ?> <br />
                    </p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
<?php
}
?>
  

在此代码中,创建数组并为开放的特定模型设置动态模型ID。