按钮值传递给模态

时间:2017-01-26 11:47:43

标签: javascript php jquery ajax

我有动态表,并且在按钮值中我存储每个id的值。 我需要将该id传递给模态并在模态中执行一些查询。我会通过php做到这一点。

<td><button="type" class="btn btn-success btn-sm"  onclick="getNarudzbaId(this)" value="<?php echo $r['id'];?>" data-toggle="modal" id="modal" data-target="#modal_theme_success">Pregled <i class="fa fa-play position-right"></i></a></td>

<script type="text/javascript">

     function getNarudzbaId(object) {

         var x = $(object).attr("value");

           $.ajax({
            type: "GET",
            url: "pregled.php",
            data:{name:x}, 

            success: function(data){
                 alert("return here if success")

            }
        })
        }

    </script>

实际上我想在模态窗口中传递值,该窗口位于按钮所在的同一页面上。

这是我的模态

<div id="modal_theme_success" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header bg-success">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h6 class="modal-title">Stavke naloga</h6>
        </div>
        <div class="modal-body">
        <h6 class="text-semibold">Text in a modal</h6>
        <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>

        <hr>
            <?php
                $temp = $_POST['name'];
                echo $temp;
            ?>
        <h6 class="text-semibold">Another paragraph</h6>
        <p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
        <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
        </div>  
        <div class="modal-footer">
            <button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-success">Save changes</button>
        </div>
    </div>
</div>
</div>

我以前从未向modal发送过值,非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

概念很简单。首先,你需要获得价值并在开放模态窗口之后发送请求

<button id="btn" value="<?php echo $r['id'];?>"></button>

通过jQuery获取按钮的值,发送请求

$('#btn').click(function () {
   var id = $(this).val();

   $.ajax({
      type: 'get',
      url: 'pregled.php'
      data: {'name':id},
      success: function(data) {
        // Show returned data on Modal
        $('.modal-title').text(data.name); // ex: show result on title
      }
   });
});

点不是向Modal发送值,而是根据您的问题在Modal上显示请求的数据。

答案 1 :(得分:0)

我已经用这种方式解决了它,如果发生类似的问题,它可以帮助某人。

按钮

td><button="type" class="btn btn-success btn-sm"  onclick="getNarudzbaId(this)" value="<?php echo $r['id'];?>" data-toggle="modal" id="modal" data-target="#modal_theme_success">Pregled <i class="fa fa-play position-right"></i></a></td>

脚本

<script type="text/javascript">           
    function getNarudzbaId(object) {
        var x = $(object).attr("value");
        $.post('modal-body.php', {post:x}, 
        function(data) {
            $('#print').html(data);  // div where value is printed
        });
    }
</script>