2017-07-28 143 views
0

我之前讀寫這個問題,其他的問題,但沒有工作,我檢查JavaScript加載完成後,我嘗試使用jQuery的是我過去$但沒有..的Javascript遺漏的類型錯誤:showcreate1不是一個函數

我的問題是ajax不工作,錯誤是標題問題。

我有一些看法,menu.blade.php代碼:

@extends('cms.public.layouts.default') 
@section('content') 

<div class="col-md-10"> 
    <h3 style="letter-spacing:40px;text-align:center;color:f15d5e;">PROYECTOS</h3> 
</div> 
<div class="col-md-2" style="padding:20px;"> 
    <button type="button" id="buttoncreate" class="btn btn-danger">Crear Proyecto</button> 

</div> 
<div class="col-md-12" id="ajaxwindow"> 

</div> 

<script> 

$(document).ready(function(){ 
     listProject(); 
     $("#buttoncreate").click(function(e){ 
     $("#buttoncreate").remove(); 
     e.preventDefault(); 
     listUploadProject(); 
     }); 
    }); 

     var listProject = function() 
     { 
     $.ajax({ 
      type:'get', 
      url:"{{ url('admin/project/listall') }}", 
      success: function(data){ 
      $('#ajaxwindow').empty().html(data); 
      } 
     }); 
     } 
     var listUploadProject = function() 
    { 
     $.ajax({ 
     type:'get', 
     url:"{{ url('admin/project/create') }}", 
     success: function(data){ 
      $('#ajaxwindow').empty().html(data); 
     } 
     }); 
    } 
</script> 
@stop 

在此視圖中,我使用Ajax和正在工作時,它示出了該圖(listall.blade.php):

<table class="table"> 
    <thead style="color:white"> 
     <tr> 
     <th>Id</th> 
     <th>Slug</th> 
     <th>Order</th> 
     <th>Public</th> 
     <th>Path header</th> 
     <th>Path home</th> 
     <th>Fecha creación</th> 
     <th>Fecha ultima actualización</th> 
     <th><span class="glyphicon glyphicon-cog"></span></th> 
     </tr> 
    </thead> 
    <tbody style="color:white"> 
    @foreach ($projects as $key => $project) 
     <tr> 
     <th>{{$project->id}}</th> 
     <td>{{$project->slug}}</td> 
     <td>{{$project->order}}</td> 
     <td>{{$project->public}}</td> 
     <td>{{$project->pathheader}}</td> 
     <td>{{$project->pathhome}}</td> 
     <td>{{ date('M j, Y', strtotime($project->created_at))}}</td> 
     <td>{{ date('M j, Y', strtotime($project->updated_at))}}</td> 
     <td><a href="{{ route('admin.projects.show', $project->id)}}" class="btn btn-info btn-sm">View</a> <a href="{{ route('admin.project.edit', $project->id)}}" class="btn btn-success btn-sm">Edit</a> 
    @endforeach 
     </tr> 
    </tbody> 
    </table> 
    <br><br> 

如果我按鈕點擊創建他去這個觀點(createproject.blade.php):

<div class="col-md-8 col-md-offset-2" id="createproject"> 

</div> 
<br><br><br> 

<script> 
$(document).ready(function(){ 
     showcreate1(); 
    }); 

     var showcreate1 = function() 
     { 
     $.ajax({ 
      type:'get', 
      url:"{{ url('admin/project/createform1') }}", 
      success: function(data){ 
      $('#createproject').empty().html(data); 
      } 
     }); 
     } 
</script> 

網址「/管理/項目/ createform1」我s由控制器調用:

public function CreateProjectForm1() 
    { 
     return view('cms.public.views.projects.createprojectform1'); 
    } 




<div class="col-md-10"> 
     <h3>Crear nuevo proyecto</h3> 
     </div> 
     <div class="col-md-2"> 
     <h3>1/2</h3> 
     </div> 
     <hr> 


     <div class="col-md-12" id="formcreateproject"> 

      <form method="POST" action="{{ route('admin.projects.store') }}" enctype="multipart/form-data"> 
       <div class="form-group"> 
       <label name="title">Slug:</label> 
       <input type="text" id="slug" name="slug" placeholder="ejemplo-de-slug" class="form-control form-control-sm"> 
       <label name="order">Order:</label> 
       <input type="number" id="order" name="order" class="form-control form-control-sm"> 
       <label name="public">Public:</label> 
       <input type="number" id="public" name="public" class="form-control form-control-sm"> 
       <label name="body">Header</label> 
       <input type="file" name="pathheader" id="pathheader" class="form-control-file" aria-describedby="fileHelp"><br> 
       <label name="body">Home</label> 
       <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="fileHelp"><br> 

       <input type="submit" value="Crear Proyecto" id="createprojectsubmit" class="btn btn-danger btn-md"> 
       <input type="hidden" name="_token" value="{{ Session::token() }}"> 
       <br><br><br> 

       </div> 
      </form> 

      </div> 

爲什麼最後一個視圖沒有加載?

非常感謝,任何幫助將不勝感激。

回答

1

嘗試使用此

showcreate1 = function() 
     { 
     $.ajax({ 
      type:'get', 
      url:"{{ url('admin/project/createform1') }}", 
      success: function(data){ 
      $('#createproject').empty().html(data); 
      } 
     }); 
     } 

沒有變種。用var聲明它會限制它的範圍。除去VAR將使其可在頁面訪問的任何地方全局函數,或做到這一點,而不是

$(document).ready(function(){ 

     var showcreate1 = function() 
     { 
     $.ajax({ 
      type:'get', 
      url:"{{ url('admin/project/createform1') }}", 
      success: function(data){ 
      $('#createproject').empty().html(data); 
      } 
     }); 
     } 
     showcreate1(); 
    }); 
+0

沒有欄錯誤更改,現在retorn是showcreate1沒有被聲明 –

+0

第二個選項是成功的,也許渲染視圖和函數沒有加載?或者,爲什麼,如果你首先聲明瞭這個函數,它的工作原理? –

+0

你的聲明中的'var'實際上使它成爲一個局部變量,這個變量對於你的用法是不可訪問的,這個變量是在頁面準備就緒時執行的函數中。 請標記答案,如果正確:D – perseusl

0

showcreate1不是一個函數,因爲您在定義它之前調用它。 將var showcreate1 = function() {}更改爲function showcreate1() {}或者只是將文檔就緒代碼移到函數定義的下面。

+0

在JavaScript您在代碼中聲明它 – taha

+0

是,使用函數聲明之前,你可以使用一個函數,而不是函數表達式。 – user1121487

+0

爲真。函數表達式不會被懸掛,所以它們不能被使用,直到它們出現在代碼中 – taha

相關問題