如何传递所选行的值以弹出警报?

问题描述 投票:0回答:1

我有一个包含值的表。因此,每一行都有重复的按钮。当用户按下重复按钮时,它将把值发送到sweetalert2弹出窗口。但是,它没有价值。我已经在sweetalert的文本中调用了{{item->description}},但是没有传递值吗?如何通过?我在重复按钮处通过了{{item->id}},但是它不起作用。

html

<table class="table table-striped">
        <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Description</th>
                <th scope="col">Plan</th>
                <th scope="col">From</th>
                <th scope="col">To</th>
                <th scope="col">Amount (RM)</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
            <?php $count = 1; ?>
            @foreach ($packages as $item)

            <tr>
                <th scope="row">{{$count++}}</th>
                <td>{{$item->description}}</td>
                <td>{{$item->Plan->name}}</td>
                <td>{{$item->from}}</td>
                <td>{{$item->to}}</td>
                <td>{{$item->price}}</td>
                <td>
                <a class="btn btn-xs btn-dark" href="/admin/insurance/product/package/edit/{{$product->id}}/{{$item->id}}">Edit</a>
                    <a class="deletePlanRecord btn btn-xs btn-danger" data-id="{{ $item->id }}"
                        id="delete" href="#">Delete</a>
                <a class="duplicatePlanRecord btn btn-xs btn-danger" data-id="{{ $item->id }}"
                            id="delete1" href="#">Duplicate</a>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>

javascript

$( ".duplicatePlanRecord" ).click(function(e) {

   Swal.fire({
   title: 'Are you sure to duplicate the plan?',
   text: 'try {{$item->description}}',
   input: 'text',
   inputAttributes: {
       autocapitalize: 'off'
   },
   showCancelButton: true,
   confirmButtonText: 'Look up',
   showLoaderOnConfirm: true,
   preConfirm: (login) => {
       return fetch(`//api.github.com/users/${login}`)
           .then(response => {
              if (!response.ok) {
                throw new Error(response.statusText)
              }
              return response.json()
            })
        .catch(error => {
          Swal.showValidationMessage(
            `Request failed: ${error}`
          )
        })
    },
javascript php jquery html sweetalert2
1个回答
0
投票

向重复的按钮链接添加另一个数据属性:-

<a class="duplicatePlanRecord btn btn-xs btn-danger" data-id="{{ $item->id }}" data-description="{{ $item->description }}" href="javascript:void(0);">Duplicate</a>

和:

$( ".duplicatePlanRecord" ).click(function(e) {
    var id = $(this).data('id'); //get id
    var desc = $(this).data('description'); //get description
    Swal.fire({
        title: 'Are you sure to duplicate the plan?',
        text: 'try id', // or use 'try desc'
        .... rest of the code

注意:-如果'try id''try desc'不起作用,请使用'try '+id'try '+desc

© www.soinside.com 2019 - 2024. All rights reserved.