【问题标题】:Laravel 4 different pages @section are breaking scriptsLaravel 4 个不同的页面@section 正在破坏脚本
【发布时间】:2015-01-23 00:07:04
【问题描述】:

我对 laravel 4 非常陌生,我正在尝试用另一个视图扩展我的布局。

我的 layout.blade.php 末尾有这个:

 {{ HTML::script('js/jquery.nicescroll.min.js') }}
 {{ HTML::script('js/jquery.pickmeup.min.js') }}
 {{ HTML::script('js/myscript.js') }}
 </body>
 </html>

然后我有一个文件 page1.blade.php,其中有很多内容,仅显示要点:

@extends('layout/layout')

@section('contents')

<section id="start">
......
</section>
@stop

在这个文件中,myscript.js 中的所有 javascript 函数都可以正常运行,尽管它们只包含在 layout.blade.php 中


现在我创建了另一个视图,名为“bookings”,并带有相应的文件“bookings.blade.php”

这里是 bookings.blade.php

@extends('layout/layout')

@section('contents')


<section id="bookingform">
<div id="bookingforma" style="background-color: #00a651; height: 10px; width:100%;">

   {{ Form::open(array('url' => 'bookings', 'class' => 'form-inline', 'role' => 'form')) }}
         <div class="form-group">
         <span><strong>Date available?  </strong>  </span>
           {{ Form::text('from1',Input::get('from'),array('class' => 'form-control', 'id' => 'fromforma')) }}
         </div> <span><strong>  -  </strong></span>
         <div class="form-group">
           {{ Form::text('to1',Input::get('to'),array('class' => 'form-control', 'id' => 'toforma')) }}
         </div> <span><strong>  for  </strong></span>
         <div class="checkbox">
           {{ Form::select('persons1', array('1' => '1','2'=>'2','3'=>'3','4'=>'4'),Input::get('persons'),array('class' => 'form-control')) }}

         </div>
     {{ Form::submit('Request!', array('class' => 'btn btn-success')) }}
       {{ Form::close() }}
</div>
</section>
@stop

基本上我对@section 和@stop 和@extends 的操作与在page1.blade.php 中的操作完全相同,但我不能使用任何javascript 函数

确切地说,如果我调用一个

 var a = document.getElementById('bookingform');

使用 myscript.js,javascript 也会中断 page1.blade.php 的内容。 (并且在 bookings.blade.php 中不起作用)

我的 routes.php 文件如下:

Route::get('/', function()
{
    return View::make('page1');
});
Route::get('index', function() {

    return View::make('page1');

});


Route::get('bookings', 'BookingController@getBooking');

Route::post('bookings','BookingController@getBookingDates');

BookingController

class BookingController extends BaseController {


    public function getBooking(){

        return View::make('bookings');
    }


    public function getBookingDates()
    {


        $data = Input::all();


        return View::make('bookings');
    }
}

关于 laravel 有什么我完全不明白的吗?或者有人发现了问题所在?

编辑:

javascript:

$(document).ready(function () {

    function heightsetter() {
        var w = window,
            d = document,
            e = d.documentElement,
            g = d.getElementsByTagName('body')[0],

            y = w.innerHeight || e.clientHeight || g.clientHeight;

        return y;
    }

    var resizeTimeout;
    window.onresize = function () {
        clearTimeout(resizeTimeout);
        var height = document.getElementById('start');
        height.style.height = heightsetter() + "px";
        var heightz = document.getElementById('hotels');
        heightz.style.height = heightsetter() + "px";
        var heightd = document.getElementById('training');
        heightd.style.height = heightsetter() + "px";

        resizeTimeout = setTimeout(function () {

        }, 250); 
    };

    var height = document.getElementById('start');
    height.style.height = heightsetter() + "px";
    var heightz = document.getElementById('hotels');
    heightz.style.height = heightsetter() + "px";
    var heightd = document.getElementById('training');
    heightd.style.height = heightsetter() + "px";
    var heightb = document.getElementById('bookingform'); //<<< **this breaks it**
    heightb.style.height = heightsetter() + "px";

    .....



    });

【问题讨论】:

  • 您是否将var a = document.getElementById('bookingform'); 推迟到 DOM 准备好之后?您的控制台中是否出现任何错误?
  • 它在 $(document).ready(function() 中被调用,是的。我在控制台中得到的错误是 Uncaught TypeError: Cannot read property 'style' of null -- for page1 (the一个通常工作)并且在预订中我得到:未捕获的类型错误:无法读取 null @BenHarold 的属性“样式”
  • 这就是您的 javascript 无法正常工作的原因。你能添加你的javascript代码吗?此错误意味着您正在引用一个不存在的元素 btw
  • 已添加,我知道这会带来错误,但我认为错误应该在两个页面上。好吧,现在是意大利早上 6 点,所以我可能没有得到一些非常基本的东西......
  • 太好了,开始工作了。非常感谢@JonathanCrowe。总是小事。

标签: javascript php laravel laravel-4


【解决方案1】:

看起来您从未将该 ID 添加到表单本身。您正在尝试访问不存在的东西。

Form::open(array('url' => 'bookings', 'class' => 'form-inline', 'role' => 'form', 'id'=>'bookingForm')

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 2013-09-05
    • 2023-03-27
    • 2019-04-25
    • 1970-01-01
    • 2020-04-10
    • 2018-02-23
    • 2021-04-17
    相关资源
    最近更新 更多