具有数据属性的open modal无法正常工作

时间:2018-05-16 23:41:54

标签: php jquery laravel

我在下面的代码中显示了有关特定国会注册的表信息:

@foreach($conference->registrations as $registration)
    <tr>
        <td>{{$registration->customer->name}} {{$registration->customer->surname}}</td>
        <td>{{ $registration->participants->count() }}</td>
        ...
        <td>
        <a class="btn btn-primary" id="showDetails"  data-regid="{{$registration->id}}">Details</a>
        </td>
</tr>
@endforeach

但是,“详细信息”链接是打开它在此页面中的模态,因此可以显示该特定注册的详细信息。但由于每个注册都需要有自己的模式来显示其具体细节,我将模态放在foreach中并添加注册ID,如“id =”registrationDetails - {{$ registration-&gt; id}}“。

但是当在任何行中单击“详细信息”链接时,它不能正常工作。

你知道问题出在哪里吗?

模态代码:

<div class="modal fade"  id="registrationDetails-{{ $registration->id }}" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="container">
          <div class="row">

            <!-- User that did the registration -->
            <dl>
              <dt>Name</dt>
              <dd>{{ $registration->customer->name }}</dd>
              <dt>Email</dt>
              <dd>{{ $registration->customer->email }}</dd>
            </dl>

            @foreach ($registration->participants as $participant)

            <!-- Participant N -->
            <dl>
              <dt>Name</dt>
              <dd>{{ $participant->name }}</dd>
              <dt>Surname</dt>
              <dd>{{ $participant->surname }}</dd>
              <dt>Ticket Type</dt>
              <dd>{{ $participant->ticket_type->name }}</dd>
            </dl>

            @endforeach
          </div>

        </div>
      </div>
      <div class="modal-footer">
        <button type="button" id="close_login_modal" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

jQuery的:

   @section('scripts')

    <script type="text/javascript" src="{{url('js/jquery-ui.js')}}"></script>

    <script type="text/javascript">
        $(function() {

            $(".showDetails").on("click", function () {
                alert("test");
                let regID = $(this).data('regid');
                alert(regID);

                $('#registrationDetails-'+regID).modal('show');
            });

        });

        </script>
@endsection

1 个答案:

答案 0 :(得分:1)

DOM ID应该是唯一的,但您有多个#showDetail ID。

将其更改为班级:

@foreach($conference->registrations as $registration)
    <tr>
        <td>{{$registration->customer->name}} {{$registration->customer->surname}}</td>
        <td>{{ $registration->participants->count() }}</td>
        ...
        <td>
        <a class="btn btn-primary showDetails" data-regid="{{$registration->id}}">Details</a>
        </td>
</tr>
@endforeach

$(".showDetails").on("click", function () {
    let regID = $(this).data('regid');
    alert(regID);

    $('#registrationDetails-'+regID).modal('show');
});