【问题标题】:laravel - can not get data-id from modallaravel - 无法从模态获取数据ID
【发布时间】:2021-06-25 23:21:21
【问题描述】:

我是 Laravel 和 jQuery/Ajax 的初学者。我想从模式的按钮中获取“数据 ID”,但是当我在控制台中看到“未定义”问题时,我可以顺便获取“名称”值。我的问题在哪里?

我的刀片是:

@foreach($doctors as $doctor)
      <a href="javascript:void(0)" type="button" class="btn btn-primary"  data-toggle="modal" data-target="#appointmentModal" ></a> 
@endforeach

<div class="modal fade" id="appointmentModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Appointment</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>

        <div class="modal-body">
            <form  id="addAppointment" novalidate>
                @csrf
                <div class="mb-3">
                    <label>Name</label>
                    <input type="text" class="form-control" id="nameR" name="name" required>
                </div>

                <button type="submit" class="btn btn-primary" data-id="{{$doctor->id}}">Save</button>
                </form>
        </div>
    </div>
    </div>
</div>

我的 jQuery/Ajax 是:

$('#addAppointment').submit(function(e){
        e.preventDefault();

        var id = $(this).attr('data-id');
        let name = $("#nameR").val();

        console.log(id);
        
        $.ajax({
            url: "{{route('add_appointmentPost')}}",
            type:"POST",
            data:{
                name:name,
                _token:'{{ csrf_token() }}',
            },
            success:function(response){
                if(response){
                    $('#addAppointment')[0].reset();
                }
            }
        });


    });

【问题讨论】:

    标签: jquery ajax laravel


    【解决方案1】:
    @foreach($doctors as $doctor)
          <a href="javascript:void(0)" type="button" class="btn btn-primary openModal" data-id="{{$doctor->id}}"></a> 
    @endforeach
    
    <div class="modal fade" id="appointmentModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Appointment</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                </div>
    
                <div class="modal-body">
                    <form  id="addAppointment" novalidate>
                        @csrf
                        <div class="mb-3">
                            <label>Name</label>
                            <input type="text" class="form-control" id="nameR" name="name" required>
                        </div>
                        <input type="hidden" id="docterId" />
                        <button type="submit" class="btn btn-primary">Save</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
        $(document).on('click', '.openModal', function () {
            var id = $(this).data('id');
            $('#docterId').val(id);
            $('#appointmentModal').modal('show');
        })
    
        $('#addAppointment').submit(function(e){
            e.preventDefault();
    
            var id = $('#docterId').val();
            let name = $("#nameR").val();
    
            console.log(id);
            
            $.ajax({
                url: "{{route('add_appointmentPost')}}",
                type:"POST",
                data:{
                    name:name,
                    _token:'{{ csrf_token() }}',
                },
                success:function(response){
                    if(response){
                        $('#addAppointment')[0].reset();
                    }
                }
            });
        });
    

    【讨论】:

    • 当我点击按钮时,我看到所有按钮都有相同的 id,所有按钮必须彼此不同。
    • 您需要手动打开引导模式。然后在表单的隐藏字段中设置 docter id。
    猜你喜欢
    • 2021-05-16
    • 1970-01-01
    • 2020-05-30
    • 2021-05-15
    • 1970-01-01
    • 2020-12-28
    • 2021-07-02
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多