如何使用JS的GET方法提交表单

时间:2019-06-12 07:49:41

标签: javascript laravel

我正在尝试通过js传递网址中的值,但出现以下错误

Uncaught RangeError: Maximum call stack size exceeded
    at String.replace (<anonymous>)

JavaScript

$(document).on('submit','#adminProductFilter',function () {
            var category_id = $('#category').val();
            var prdouct_name = $('#productName').val();
            var link = '{{url('/admin/products?:id')}}';
            var url = link.replace(':id',"categories_id="+category_id+"&product="+prdouct_name);
            $(this).attr('action',url);
            $('#adminProductFilter').submit();
        });

Form.php

<form class="form-inline form-validate" enctype="multipart/form-data" id="adminProductFilter">
                        {{csrf_field()}}
                      <div class="form-group">
                        <h5 style="font-weight: bold; padding:0px 5px; ">{{ trans('labels.FilterByCategory/Products') }}:</h5>
                      </div>
                      <div class="form-group" style="min-width: 220px">
                        <select class="form-control" name="categories_id" style="width: 100%" id="category">
                            <option value="">{{ trans('labels.SelectCategory') }}</option>
                            @foreach ($results['subCategories'] as  $key=>$subCategories)
                                <option value="{{ $subCategories->id }}"
                                    @if(isset($_REQUEST['categories_id']) and !empty($_REQUEST['categories_id']))
                                        @if( $subCategories->id == $_REQUEST['categories_id'])
                                            selected
                                        @endif
                                    @endif
                                >{{ $subCategories->name }}</option>
                            @endforeach
                        </select>
                      </div>
                      <div class="form-group">
                        <input type="text" name="product" class="form-control" id="productName"
                            @if(isset($_REQUEST['product']) and !empty($_REQUEST['product']))
                                value="{{ $_REQUEST['product'] }}"
                            @endif
                         placeholder="Products">
                      </div>
                      <button type="submit" class="btn btn-success">{{ trans('labels.Search') }}</button>
                      <a href="{{ URL::to('admin/products')}}" class="btn btn-danger">{{ trans('labels.ClearSearch') }}</a>
                    </form>

Route.php

Route::get('/products', 'AdminMasterProductsController@products');

当我单击Submit(提交)按钮时,它在表单操作中显示url,但表单未提交。

我只想提交表格并得到结果。

请帮助。

1 个答案:

答案 0 :(得分:0)

您可以将Submit button的属性更改为'button',并为其指定一个ID。

<form class="form-inline form-validate" enctype="multipart/form-data" id="adminProductFilter">
                        {{csrf_field()}}
                      <div class="form-group">
                        <h5 style="font-weight: bold; padding:0px 5px; ">{{ trans('labels.FilterByCategory/Products') }}:</h5>
                      </div>
                      <div class="form-group" style="min-width: 220px">
                        <select class="form-control" name="categories_id" style="width: 100%" id="category">
                            <option value="">{{ trans('labels.SelectCategory') }}</option>
                            @foreach ($results['subCategories'] as  $key=>$subCategories)
                                <option value="{{ $subCategories->id }}"
                                    @if(isset($_REQUEST['categories_id']) and !empty($_REQUEST['categories_id']))
                                        @if( $subCategories->id == $_REQUEST['categories_id'])
                                            selected
                                        @endif
                                    @endif
                                >{{ $subCategories->name }}</option>
                            @endforeach
                        </select>
                      </div>
                      <div class="form-group">
                        <input type="text" name="product" class="form-control" id="productName"
                            @if(isset($_REQUEST['product']) and !empty($_REQUEST['product']))
                                value="{{ $_REQUEST['product'] }}"
                            @endif
                         placeholder="Products">
                      </div>
                      <button type="button" id='btn-submit' class="btn btn-success">{{ trans('labels.Search') }}</button>
                      <a href="{{ URL::to('admin/products')}}" class="btn btn-danger">{{ trans('labels.ClearSearch') }}</a>
                    </form>

并通过事件点击提交。

$("#btn-submit").click(function(){
         var category_id = $('#category').val();
            var prdouct_name = $('#productName').val();
            var link = '{{url('/admin/products?:id')}}';
            var url = link.replace(':id',"categories_id="+category_id+"&product="+prdouct_name);
            $("#adminProductFilter").attr('action',url);
            $('#adminProductFilter').submit();
})