如何使用Ajax和多个复选框从控制器获取结果 [英] How to get result from controller using ajax and multi checkboxes

查看:110
本文介绍了如何使用Ajax和多个复选框从控制器获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用复选框的值数组从ajax获取结果,我是Laravel和ajax的新手,我试图制作一个过滤器以使用复选框显示一些数据,在这段代码中,我试图实现它仅复选框的值而不是显示控制器的结果,请在ajax和控制器中需要帮助以解决此问题 我是从另一个关于stackoverflow的问题中获得此代码的,但是缺少一些东西,我不知道它到底在哪里

How can I get results from ajax using array of values from checkboxes I'm new to Laravel and ajax, I'm trying to make a filter to show some data using checkboxes, in this code which i tried to implement it shows only the value of the checkbox instead of showing the results from controller, please I need help in ajax and controller to solve this issue I got this code from another question on stackoverflow but there is something missing I don't know where is it exactly

刀片

@foreach
    <div class="custom-radio">
        <input type="checkbox"  class="batchCheckbox" name="batch[]" value=" {{$item->indivgroup_option}}">
        <label>
            <span>
                {{ ($item->indivgroup_option =='i')?"Individual Tour":"Group Tour" }}
            </span>
        </label>
    </div>
@endforeach  

jQuery

var categories;
var getfilters = function () {
    $.ajax({
        type: 'POST',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'{{URL::to('offer-category')}}',
        data: {categories: categories},
        error: function(e) {
            console.log(e.responseText);
        },
        success: function (data) {
            console.log(data);
            $('#myResponse').html(data);
        }
    });
};


$(document).ready(function() {
    $('input[type="checkbox"]').on('change', function(event) {
        event.preventDefault();
        categories = $("input:checkbox:checked").map(function(){
            return $(this).val();

        }).get();

        $('#myResponse').html(categories.toString());
        getfilters();
    });
});

控制器

public function getoffergroup(Request $request)
{

    $input  = array(Input::get('categories'));
    $posts=Offer::whereIn('indivgroup_option' , $input)->get();
    $categories = Category::getGroup();
    return response()->view('offer.archive', compact('offers', 'categories'));
}

推荐答案

您好,请尝试从复选框中获取已检查的值,希望对您有帮助

HI try this for you get checked value from checkbox Hope its helps

<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<input type="checkbox" name="batch" value="1">
<input type="checkbox" name="batch" value="2">
<input type="checkbox" name="batch" value="3">
<input type="checkbox" name="batch" value="4">
<input type="checkbox" name="batch" value="5">

<script type="text/javascript">
    var categories = [];
    var getfilters = function () {
        console.log(categories);
  $.ajax({
            type: 'POST',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },

            url:'{{URL::to('offer-category')}}',

            data: {categories: categories},
            error: function(e) {
                console.log(e.responseText);
            },
            success: function (data) {
                console.log(data);
                $('#myResponse').html(data);
        }});
    };


    $(document).ready(function() {
        $('input[type="checkbox"]').on('click', function(event) {
           event.preventDefault();
            $.each($("input[name='batch']:checked"), function(){            
                categories.push($(this).val());
            });

        $('#myResponse').html(); // <---- 'one,two,three;
             getfilters();
        });
    });
</script>

----------控制器获取数据并将数据返回给ajax -----

---------- Controller get data and return data to ajax -----

    public function getoffergroup(Request $request)
{
    $categories = $request->categories; // this is use for get request, if you are getting single value make lopp for single like :

    $posts = Offer::whereIn('indivgroup_option' , $categories)->get();

    // if you resturn result in ajax use this for 

    $categories = Category::getGroup();

    if (!empty($posts)) {
        return response()->json([
            'status'=>200,
            'message'=>"sussfully get data",
            "data" =>$posts // if you return any data in ajax use this 
            "key"=>"value" // value mean data  
        ]);
    }else{
        return response()->json([
            'status'=>404,
            'message' => "no data found"
        ]);
    }
}

这篇关于如何使用Ajax和多个复选框从控制器获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆