有什么方法可以在不在 Laravel 中写入 JQuery 的情况下对表单提交进行部分页面刷新吗?

Is there any way to do Partial page refresh on form submit without writing JQuery in Laravel?

我知道 .NET 和 PHP,据我了解,.NET 中的 EntityFramework 与 PHP 中的 Laravel 相同。

在 .NET 中,我们有 @using (Ajax.BeginForm 呈现与 PHP 中的 {!! Form::open 相同的表单。

@using (Ajax.BeginForm 不会在表单提交时刷新整个页面,我不需要写 JQuery 来执行此操作。所以,基本上它的作用与 JQuery.ajax

相同

问题:有什么方法可以在不在Laravel中写入JQuery的情况下在表单提交时进行部分页面刷新?

奖金问题 我们还可以选择传递参数,例如 onSuccess、OnComplete、OnBegin 和用于传递表单 css class 的参数。我们可以在 Laravel 中执行此操作吗?这在 .NET

中的 Entity Framework 中是可能的

简单地说:没有

但是,您可以创建自己的包来为您执行此操作。

创建一个简单的 class 来打开和关闭您的表单:

namespace Ajaxform;
Class AjaxForm {

    public function begin($action, $method, $options){
        //do other stuff with options, minimal example
        return '<form action="'.$action.'" method="'.$method.'" data-ajax-form/>';
    }
    public function end(){
        return '</form>';
    }
}

然后创建 Facade 访问器:

namespace AjaxForm\Facades;
Class AjaxFormFacade extends Illuminate\Support\Facades\Facade{
    public static function getFacadeAccessor(){
        return 'ajaxform';
    }
}

然后创建一个将我们的外观绑定到应用程序的服务提供者:

namespace AjaxForm\Providers;
public function AjaxFormServiceProvider {

    public function bind(){
        $this->publishes([
            __DIR__.'/../assets/' => public_path('vendor/ajaxform')
        ], 'public');
    }

    public function register(){
        $this->app->bind('ajaxform', function(){
            return new AjaxForm;
        }
    }
}

您的供应商包看起来像这样:

MyVendor (replace this with whatever you want)
    - Src
        - AjaxForm
            - assets
                 ajax-form.js
            - Facades
                 AjaxFormFacade.php
            - Providers
                 AjaxFormServiceProvider.php
            AjaxForm.php

您的 composer.json 将直接在您的 AjaxForm 中看起来像这样(父级,位于 Src 目录之前)

{
    "name": "myvendor/ajax-form",
    "version" : "master",
    "description": "An Ajax Form Wrapper.",
    "license" : "MIT",
    "keywords": ["ajaxform"],
    "authors" : [
        {
            "name": "My Name",
            "email": "wheredoigetahold@of.you"
        }
    ],
    "require": {
        "php": ">=5.5",
        "illuminate/support": "4.*|5.*"
    },
    "autoload": {
        "psr-4": {
            "Ajaxform\": "src/AjaxForm/"
        }
    },
  "minimum-stability": "stable"
}

现在您已经创建了供应商包,将其添加到 config/app.php 文件并告知 IOC 我们的新外观和提供者:

找到 providers 数组并在末尾添加:

MyVendor\AjaxForm\AjaxForm::class

然后在 Facades 数组的末尾添加:

'AjaxForm' => 'MyVendor\AjaxForm\AjaxForm::class

现在您只需要创建 ajax-form.js 文件,该文件将包含我们对表单进行 ajax 化的逻辑。

jQuery(function($){
    $('form[data-ajax-form]').on('submit', function(e){
        e.preventDefault();
        var $this = $(this);

        $.ajax({
            method: $this.prop('method'),
            url: $this.prop('action'),
            data: $this.serialize()
        }).done(function(resp){
            //put some logic here
        }).error(function(err){
            //put some logic here
        });
    });
});

现在您可以调用表单了:

{{AjaxForm::begin(route('path.to.my.endpoint'), 'POST') }}
    //form inputs go in here
{{AjaxForm::end()}}