You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

循环生成的带data属性模态框点击无响应问题排查

问题排查与修复方案

我帮你找出了几个导致模态框无法弹出的核心问题,以及对应的修复方案:

1. 重复的ID属性(最关键问题)

你在循环里给每一个「Details」按钮都设置了相同的id="showDetails",但HTML规范要求ID必须是页面唯一的。浏览器只会识别第一个带该ID的元素,所以后面的按钮点击根本不会触发绑定的jQuery事件。

同时你的jQuery选择器也写错了:$("showDetails")没有加#前缀,就算ID是唯一的,也无法选中对应的按钮元素。

2. 模态框关闭按钮的重复ID(潜在问题)

循环生成的每个模态框里,关闭按钮都用了id="close_login_modal",同样违反了ID唯一的规则——虽然暂时不影响弹出,但后续如果针对这个ID写逻辑时一定会出错。


修复后的代码示例

第一步:修改循环中的按钮代码

把按钮的id="showDetails"改成class="btn btn-primary showDetails",用类选择器来绑定事件:

@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

第二步:修改模态框的关闭按钮(可选但推荐)

把关闭按钮的id="close_login_modal"改成class="btn btn-primary close-modal":

<div class="modal fade" id="registrationDetails-{{ $registration->id }}" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <!-- 其他模态框内容保持不变 -->
  <div class="modal-footer">
    <button type="button" class="btn btn-primary close-modal" data-dismiss="modal">Close</button>
  </div>
</div>

第三步:修正jQuery事件绑定代码

把选择器改成类选择器.showDetails,确保语法正确:

@section('scripts')
<script type="text/javascript" src="{{url('js/jquery-ui.js')}}"></script>
<script type="text/javascript">
  $(function() {
    // 用类选择器绑定所有详情按钮的点击事件
    $(".showDetails").on("click", function () {
      let regID = $(this).data('regid');
      // 打开对应ID的模态框
      $('#registrationDetails-' + regID).modal('show');
    });
  });
</script>
@endsection

这样修改后,每个按钮点击都会正确触发事件,打开对应的模态框啦~

内容的提问来源于stack exchange,提问作者johnW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:13:08