douju8113 2017-04-20 11:06
浏览 59
已采纳

将数据id传递给bootstrap模式时出现问题

I have a listing page containing a link button to show modal repeating in a PHP loop. I have searched and found these: Passing data to a bootstrap modal, Passing data to Bootstrap 3 Modal and Pass id to Bootstrap modal and some others but failed.

What I want

I have fetched the data id from database and pass it to bootstrap modal and there I will use this id to display more info through MySQL queries.

I have written all the code but the ID is not passing to the modal.

Modal Link Button code

 <a class="btn btn-warning btn-minier" href="#modal-form-show" role="button" data-toggle="modal" data-target="#myModal" data-id="<?php echo $cottage_id ?>"><i class="icon-edit bigger-120"></i> Reservation </a>

Modal code

 <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Reservation Details</h4><br>
            <h5>Cottage Id :</h5>
            <h5>Cottage Title :</h5>

          </div>
          <div class="modal-body">
            <p>ID WILL SHOW IN THIS TEXT FIELD.
           <input type="text" name="cottage" placeholder="cottage id" value="" id="cottage" /> 
            </p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>

JS code

$('#modal-form-show').on('show.bs.modal', function(e) {
  var c_id= $('a[href="#modal-form-show"]').data('id');
  //var c_id= $(this).data(id) - checked both
  $("#cottage").val(c_id);
});

I have taken a text input in modal named cottage and I want show that id in this input.

  • 写回答

3条回答 默认 最新

  • douque8855 2017-04-20 12:20
    关注

    I have change little , when button click show the popup & i've set the value of data-id=2

    Button

    <a class="btn btn-warning btn-minier" id="btnModalPopup" href="" role="button" data-toggle="modal" data-target="" data-id="2"><i class="icon-edit bigger-120"></i> Reservation </a>
    

    I add button in you modal

        <div id="myModal" class="modal fade" role="dialog">
            <div class="modal-dialog">
    
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Reservation Details</h4><br>
                        <h5>Cottage Id :</h5>
                        <h5>Cottage Title :</h5>
    
                    </div>
                    <div class="modal-body">
                        <p>
                            ID WILL SHOW IN THIS TEXT FIELD.
                            <input type="text" name="cottage" placeholder="cottage id" value="" id="cottage" readonly />
                        </p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-success" data-dismiss="modal" id="btnSubmit">Submit</button>
    
                    </div>
                </div>
            </div>
        </div>
    

    Javascript code

    <script>
            $(document).ready(function () {
                $('#btnModalPopup').click(function () {
    
                    var c_id = $(this).data('id');
                    console.log(c_id)
    
                    $('#myModal #cottage').val(c_id)
                    $('#myModal').modal('show')
    
                });
    
       $('#btnSubmit').click(function () {
    
            alert('submit button click')
    
            // get the id , from input field.
    
            var id = document.getElementById('cottage').value;
            console.log(id)
            alert(id)
    
            //You can set the id - to php variable - then hit to sever by ajax
        })
            });
    
    
        </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序