我们如何在select中使用Laravel Jquery onchange元素?

时间:2016-05-23 07:40:06

标签: javascript jquery laravel

<script>
    $('#brand_id').on('change',function(e){
        console.log(e);
        var brand_id =  e.target.value;

        //ajax
        $.get('/product_id?brand_id =' + brand_id, function(data));
        //success data
        $.each(data, function(upload_form, product_cat){
            $('product_category').empty();
            $('product_category').append('<option value="'+ product_cat.id +'">'+ product_cat.product_hierarchy +'</option>');
        });

    });

上面是我的Jquery代码检查这个,并指出我是什么问题。

<form enctype="multipart/form-data" action="{{action('BrandController@upload_csv')}}" method="post">
    Brand Code
    <select name="brand_id" id="brand_id" Select="" class="form-control">
      <option value="">--Select Brand Code--</option>
      <?php foreach ($brands as $row) { ?>
      <option value="<?= $row->brand_id ?>"><?= $row->brand_name ?></option>

      <?php } ?>

    </select>
    Product Id
    <select name="product_category" id="product_category"   Select="" class="form-control">

    </select>

    Status
    <select name="status" Select="" class="form-control">
      <option value="">--Select Product Status--</option>

      <option value="1">Yes</option>
      <option value="0">No</option>

    </select>

    <input type="hidden" name="_token" value="<?= csrf_token(); ?>">
    Upload Valid csv file
    <input type="file" name="csv" required class="form-control">
    <br/>
    <input type="submit" value="Upload" class="btn btn-primary">
</form>

这是我的表格。在表单中有一个选择,其中onchange元素我想在下一个选择中显示子类别。下面是我的路线文件代码。

Route :: get('/ product_id',function(){     $ brand_id =输入:: get('brand_id');

$product_id = product_hierarchy::where('bcategory_code','=',1)->get();

return Response::json($product_id);

});

1 个答案:

答案 0 :(得分:1)

Laravel 5.2中,我们必须传递csrf令牌来执行请求。

试试这样......

<script>
        $('#brand_id').on('change',function(e){
            console.log(e);
            var brand_id =  e.target.value;

            //ajax
            $.get('/product_id?brand_id =' + brand_id,{"_token":$("input[name='_token']").val()}, function(data));
            //success data
            $.each(data, function(upload_form, product_cat){
                $('product_category').empty();
                $('product_category').append('<option value="'+ product_cat.id +'">'+ product_cat.product_hierarchy +'</option>');
            });

        });
</script>

我只是将这行代码添加到您的获取请求中:

{ "_token" : $("input[name='_token']").val() }

祝你好运..快乐的编码!!!

<强>被修改

有关此内容的详细信息,请参阅以下链接教程:

Laravel CRUD Using jQuery AJAX PART – 1

Laravel CRUD Using jQuery AJAX PART – 2