You need to enable JavaScript to run this app. Laravel 8中如何将选中的产品对象传入模态框更新表单完成编辑
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8中如何将选中的产品对象传入模态框更新表单完成编辑

单模态框编辑行数据的实现方案

1. 表格行按钮绑定当前产品字段

给每行的编辑按钮添加自定义data-*属性,在Blade循环时直接将当前产品的核心字段挂载到按钮上,无需额外接口请求:

<tbody>
@foreach($ArrayProducts as $product)
    <tr>
        <td>{{$product['name']}}</td>
        <td>{{$product['description']}}</td>
        <td>{{$product['price']}}</td>                   
        <td>{{$product['stock']}}</td>                    
        <td>
            <button 
                type="button" 
                class="btn btn-primary" 
                data-toggle="modal" 
                data-target="#exampleModal"
                data-id="{{ $product['id'] }}"
                data-name="{{ $product['name'] }}"
                data-description="{{ $product['description'] }}"
                data-price="{{ $product['price'] }}"
                data-stock="{{ $product['stock'] }}"
            >
                Edit
            </button>
        </td>            
    </tr>
@endforeach   
</tbody>

注意:需要保证后端组装的$ArrayProducts中包含每个产品的唯一标识id,否则后续更新操作无法定位目标产品。

2. 修正更新表单的基础配置

原生HTML表单不支持PUT请求方法,需要使用Laravel的方法伪装指令适配RESTful路由规则,同时给表单添加固定id方便后续JS定位:

<form id="editProductForm" method="POST" action="productos">
  @csrf     
  @method('PUT')
  <div class="form-group">
      <label>Name</label>
      <input type="text" class="form-control" id="nombre" name="name" required>
  </div>
  <div class="form-group">
      <label>Descripcion</label>
      <input type="text" class="form-control" id="description" name="description" required>
  </div>
  <div class="form-group">
      <label>price</label>
      <input id="price" type="number" class="form-control" name="price" required>
  </div>         
  <div class="form-group">
      <label>Stock</label>
      <input id="stock" type="number" class="form-control" name="stock" required >
  </div>
  <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      <button type="submit" class="btn btn-primary">Save changes</button>
  </div> 
</form>

3. 监听模态框事件自动填充数据

利用Bootstrap模态框原生的show.bs.modal事件,在模态框打开瞬间读取触发按钮上绑定的产品数据,自动填充到表单项中,同时动态修改表单的提交地址,拼接当前编辑的产品id:

$('#exampleModal').on('show.bs.modal', function (event) {
    // 获取触发当前模态框的按钮元素
    const triggerButton = $(event.relatedTarget)
    // 读取按钮上绑定的所有产品字段
    const productInfo = triggerButton.data()
    // 获取模态框根元素
    const modalRoot = $(this)
    // 逐字段填充到对应的表单输入框
    modalRoot.find('#nombre').val(productInfo.name)
    modalRoot.find('#description').val(productInfo.description)
    modalRoot.find('#price').val(productInfo.price)
    modalRoot.find('#stock').val(productInfo.stock)
    // 动态更新表单提交地址,匹配Laravel的更新路由规则 productos/{id}
    modalRoot.find('#editProductForm').attr('action', `productos/${productInfo.id}`)
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.25 20:54:03