Laravel 5.4-如何在JavaScript加载之前让@section产生 [英] Laravel 5.4 - How to make @section yield before javascript loads

查看:124
本文介绍了Laravel 5.4-如何在JavaScript加载之前让@section产生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript动态创建一个表单,该表单从聊天中获取答案并将其添加到表单中.该表格存在于单独的刀片文件register.blade.php中.我在主页(index.blade.php)中产生了表单,但是@section('registration-form')在读取js之前产生了该页面.因此,聊天中的答案永远不会输入到表单中.

I'm using javascript to dynamically create a form that takes answers from a chat and adds it to a form. The form exists in a separate blade file register.blade.php. I yielded the form in the home page (index.blade.php) but the @section('registration-form') yields to the page before the js is read. For that reason, the answers from the chat never get input into the form.

register.blade.php保留(自定义)格式@section('registration-form').在这里,我想在routes/web.php中创建一个route::get('/index', 'CustomAuthController'),以将表单数据存储在数据库中.关于如何进行这项工作的任何想法?

register.blade.php holds the (custom) form @section('registration-form'). From there I'd like to create a route::get('/index', 'CustomAuthController') in routes/web.php to store the forms data in my database. Any ideas on how to make this work?

Laravel的新手.使用Laravel 5.4.

Very new to Laravel. Using Laravel 5.4.

代码:



layouts/app.blade.php



layouts/app.blade.php

@include('includes.head')

<body>        
    @include('includes.navbar')
    <div class="container">
        @yield('content')
    </div>
    @include('includes.footer')
</body>



index.blade.php



index.blade.php

@extends('layouts.app') 

@section('content')

<main>      
  <section class="row" >    

     <!------ a bunch of content here ----------->

     <!----- form yielded here -------->

         @yield('registration-form')

     <!--- form end ----->
     <!------ more content -------->
  </section>
</main>
@endsection

register.blade.php

register.blade.php

@section('registration-form')

<div class="padding-32" id="signup-two">
<h4>Review and submit your information</h4>
<form  method="POST" action="{{ route('signup') }}">
  {{ csrf_field() }}

  <div class="form-group" id="fname">
   <label></label>
   <input name="fname"/>
  </div>

  <div class="form-group" id="email">
   <label></label>
   <input name="email"/>
  </div>
  <div class="form-group" id="password">
   <label></label>
   <input name="password"/>
  </div>
  <div class="form-group" id="BMI">
   <label></label>
   <input name="BMI"/>
  </div>
  <div class="form-group" id="height">
   <label></label>
   <input name="height"/>
  </div>
  <div class="form-group" id="weight">
   <label></label>
   <input name="weight"/>
  </div>
</form>

@endsection

我尝试在app.blade.php中产生@section(registration-form)的形式,但该表格仍未加载.我也无法弄清楚如何为此创建路由和控制器.代码如下:

I tried yielding @section(registration-form) in app.blade.php the form still did not load. I also couldn't figure out how to create a route and a controller for this. The code looked like this:

layouts/app.blade.php

layouts/app.blade.php

@include('includes.head')

  <body>

    @include('includes.navbar')
    <div class="container">
        @yield('content')
        @yield('registration-form')
    </div>
    @include('includes.footer')
  </body>
</html>

routes/web.php

routes/web.php

Route::get('/index', function () {
  return view('registration-form', [
   'fname' => 'fname',
   'BMI' => 'BMI',
   'height' => 'height',
   'weight' => 'weight'
   ]);
});

推荐答案

通常在dom准备就绪时加载Javascript.

Javascript is usually loaded when dom is ready.

但是您想在该特定DOM(@yield(form))元素准备好之前加载脚本.

But you want to load script before that specific DOM ( @yield(form) ) element is ready.

因此,您必须执行使用ajax进行Laravel加载视图"之类的操作,周围已经有一些实际示例. 如何返回视图是从Laravel 5中的AJAX调用中获取的?

So you have to do something like "laravel load view using ajax", there are already some practical example around. How can I return a view from an AJAX call in Laravel 5?

您要为Ajax调用创建一个路由器,该控制器函数返回部分视图(因此@section('form')).在您的js中准备好必要的代码后,调用ajax.然后将视图追加到所需位置.没有容易的方法.

You want to create a router for Ajax call, controller function to return the partial view (hence @section('form')). Call the ajax after the necessary code is ready in your js. Then Append the view to desire location. There are not easy way around.

您可能想重新考虑针对当前要完成的事情进行设计.

You might want to reconsider you design for whatever you are trying to accomplish right now.

用于数据处理,计算等.如果可以通过后端完成并验证它们,只需将它们发送到后端即可.一个常见的错误是Frontend试图处理Backend任务.

For things like data manipulation, calculation, etc. If they can be done and validated by backend, simply send them to the backend. One common mistake is Frontend is trying to handle the Backend tasks.

这篇关于Laravel 5.4-如何在JavaScript加载之前让@section产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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