我有一个项目列表和任何项目的三种类型的付款我想提供所有类型的付款并将付款ID发送到ajax但我只为所有请求提供id=1我的代码在这里:
@foreach($project->payments as $payment)
@if($payment->type==1)
@if(empty($payment->code))
<input type="hidden"id="payment_id"value="{{$payment->id}}">
<input type="text" class="form-control" id="paymentCode" name="paymentC">
{{csrf_field()}}
<button id="AddCode" type=submit class="btn btn-info generate-label AddCode">enter</button>
@else
{{$payment->code}}
@endif
@elseif($payment->type==2)
@if(empty($payment->code))
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" name="paymentC" id="paymentCode">
{{csrf_field()}}
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
@else
{{$payment->code}}
@endif
@endif
@endforeach
Javascript代码:
$('.AddCode').click (function (event) {
var payment_id = $('#payment_id').val();
var paymentCode = $('#paymentCode').val();
console.log(payment_id);
});
如何解决点击付款按钮时打印此 ID 的问题?
最佳答案
您没有使用 var payment_id = $('# payment_id').val();
定位唯一 ID
更改这行html
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
进入
<button value="{{$payment->id}}" type="submit" class="btn btn-info generate-label AddCode">enter</button>
JQuery
$('.AddCode').on("click", function(event){
var payment_id = $(this).val(); // payment id
});
关于php - 将数据从输入表单传递到 Laravel 中的 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47061180/