【问题标题】:Send a value of foreach loop form using ajax in laravel在laravel中使用ajax发送foreach循环形式的值
【发布时间】:2021-09-06 07:25:31
【问题描述】:

我正在尝试将值从表单发送到控制器,但是所有表单都面临的错误将值作为第一个表单 id 发送我希望每个表单在我按下发送按钮时发送其中的数据,但是会发生什么每个人都根据表单的第一个 ID 发送其内容;

刀片代码

@forelse($matches as $match)
    <form class="ajaxform">
        @csrf
        <div class="wrapper-new1">
            <input type="text" class="form-control" name="record_one" id="validationCustom18" value="{{$match->record_one}}"  required> 
            <input type="text" class="form-control" name="record_two" id="validationCustom18" value="{{$match->record_two}}"  required> 
            <input type="hidden" class="form-control" name="id" value="{{$match->id}}" id="validationCustom18" >
        </div>
        <button type="submit" style="margin-top: 4px;" class="btns">تغيير نتيجة المباراة </button>
    </form>
    @empty 
    <div class="alert alert-danger"> لا يوجد مباريات لعرضها الان </div>   
@endforelse

ajax 代码

$('.btns').on("click", function(event){
    event.preventDefault();
  
    let record_one = $("input[name=record_one]").val();
    let record_two = $("input[name=record_two]").val();
    let id = $("input[name=id]").val();
    let _token   = $('meta[name="csrf-token"]').attr('content');
  
    $.ajax({
        url: "/admin/team/matchesList/"+id+"",
        type:"POST",
        data:{
            record_one:record_one,
            record_two:record_two,
            id :id,
            _token: _token
        },
        success:function(response){
            console.log(response);
            if(response) {
                $('.success').text(response.success);
                $(".ajaxform")[0].reset();
            }
        },
    });
});

【问题讨论】:

    标签: javascript php ajax laravel laravel-blade


    【解决方案1】:
    $('.btns').on("click", function(event){
            event.preventDefault();
            let form = $(this).closest('.ajaxform');
            let record_one = form.find("input[name=record_one]").val();
            let record_two = form.find("input[name=record_two]").val();
            let id = form.find("input[name=id]").val();
            let _token   = $('meta[name="csrf-token"]').attr('content');
      
            $.ajax({
              url: "/admin/team/matchesList/"+id+"",
              type:"POST",
              data:{
                record_one:record_one,
                record_two:record_two,
                id :id,
                _token: _token
              },
              formElement:form,
              success:function(response){
                console.log(response);
                if(response) {
                  $('.success').text(response.success);
                  this.formElement[0].reset();
                }
              },
             });
        });
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 2017-01-31
      • 2018-03-25
      • 2016-09-08
      • 1970-01-01
      相关资源
      最近更新 更多