php - 将数据从输入表单传递到 Laravel 中的 ajax

标签 php jquery ajax laravel

我有一个项目列表和任何项目的三种类型的付款我想提供所有类型的付款并将付款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/

相关文章:

php - 从 PHP 写入图像文件时出错

javascript - 在复选框单击/选中时隐藏元素,在复选框取消选中时重新插入元素

javascript - 想要将javascript变量解析到mysql数据库

javascript - 使用 Javascript History API 时,新的 URL 内容应该是什么?

ajax - Koa-Bodyparser发送POST请求时出错 "invalid JSON, only supports object and array"

php - 如何在php中显示B列中具有相同值的A列的所有结果

php - 将 session 变量存储从全部更改为个人后,登录表单不起作用

javascript - 对通过 foreach 从数据库插入到表中的行进行排序

javascript - 检查是否使用 jQuery 应用了 css

javascript - 具有功能的 Cocoon 数据关联插入节点