admin管理员组

文章数量:1426620

I have a 3 rowed table and I want to click on each row and describe more about the content of each in a modal pop out . The problem is when I click on the row the screen will go a bit darker but no pop up shows up

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href=".3.6/css/bootstrap.min.css">
  <script src=".12.2/jquery.min.js"></script>
  <script src=".3.6/js/bootstrap.min.js"></script>
  <script>
      $(function(){
          $('#orderModal').modal({
              keyboard : true,
              backdrop : "static",
              show     : false,
          }).on('show', function(){
              var getIdFromRow = $(this).data('orderid');
              //make your ajax call populate items or what even you need
              $(this).find('#orderDetails').html($('<b> Order Id selected: ' + getIdFromRow  + '</b>'))
          });

          $(".table-striped").find('tr[data-target]').on('click', function(){
             //or do your operations here instead of on show of modal to populate values to modal.
              $('#orderModal').data('orderid',$(this).data('id'));
          });
      });
  </script>
</head>
<body>
<div class="container">
    <h1>Orders</h1>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Customer</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <tr data-toggle="modal" data-id="1" data-target="#orderModal">
                    <td>1</td>
                    <td>24234234</td>
                    <td>A</td>
                </tr>
                <tr data-toggle="modal" data-id="2" data-target="#orderModal">
                    <td>2</td>
                    <td>24234234</td>
                    <td>A</td>
                </tr>
                <tr data-toggle="modal" data-id="3" data-target="#orderModal">
                    <td>3</td>
                    <td>24234234</td>
                    <td>A</td>
                </tr>
            </tbody>
        </table>
        <div id="orderModal" class="modal hide fade" role="dialog" aria-labelledby="orderModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                <h3>Order</h3>
            </div>
            <div id="orderDetails" class="modal-body"></div>
            <div id="orderItems" class="modal-body"></div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
            </div>
        </div>
    </div>
</body>
</html>

I have a 3 rowed table and I want to click on each row and describe more about the content of each in a modal pop out . The problem is when I click on the row the screen will go a bit darker but no pop up shows up

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis./ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn./bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script>
      $(function(){
          $('#orderModal').modal({
              keyboard : true,
              backdrop : "static",
              show     : false,
          }).on('show', function(){
              var getIdFromRow = $(this).data('orderid');
              //make your ajax call populate items or what even you need
              $(this).find('#orderDetails').html($('<b> Order Id selected: ' + getIdFromRow  + '</b>'))
          });

          $(".table-striped").find('tr[data-target]').on('click', function(){
             //or do your operations here instead of on show of modal to populate values to modal.
              $('#orderModal').data('orderid',$(this).data('id'));
          });
      });
  </script>
</head>
<body>
<div class="container">
    <h1>Orders</h1>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Customer</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <tr data-toggle="modal" data-id="1" data-target="#orderModal">
                    <td>1</td>
                    <td>24234234</td>
                    <td>A</td>
                </tr>
                <tr data-toggle="modal" data-id="2" data-target="#orderModal">
                    <td>2</td>
                    <td>24234234</td>
                    <td>A</td>
                </tr>
                <tr data-toggle="modal" data-id="3" data-target="#orderModal">
                    <td>3</td>
                    <td>24234234</td>
                    <td>A</td>
                </tr>
            </tbody>
        </table>
        <div id="orderModal" class="modal hide fade" role="dialog" aria-labelledby="orderModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                <h3>Order</h3>
            </div>
            <div id="orderDetails" class="modal-body"></div>
            <div id="orderItems" class="modal-body"></div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
            </div>
        </div>
    </div>
</body>
</html>
Share Improve this question edited May 24, 2016 at 19:25 Artur Filipiak 9,1574 gold badges32 silver badges56 bronze badges asked May 24, 2016 at 18:50 Danial KosarifaDanial Kosarifa 1,0744 gold badges19 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

The reason is that you're probably using the newest bootstrap and an old HTML modal format.

Your modal should include <div class="modal-dialog"> and <div class="modal-content"> elements. Also, there's no more hide class in the actual version of bootstrap

<div id="orderModal" class="modal fade" role="dialog" aria-labelledby="orderModal" 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">x</button>
        <h3>Order</h3>
      </div>
      <div id="orderDetails" class="modal-body"></div>
      <div id="orderItems" class="modal-body"></div>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
      </div>
    </div>
  </div>
</div>

Another thing is the show event, which should be on('show.bs.modal'), not on.('show')

DEMO

本文标签: javascriptModal for clickable table rows in bootstrapStack Overflow