【发布时间】: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">×</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();
}
}
});
});
【问题讨论】: