2015-09-21 59 views
0

現在我正在開發使用Laravel 5.0的電子商務購物網站,這對於我的fyp來說,還有很長的路要走。Laravel Javascript Image Swap Dropdown List

而且,我做產品展示頁面

這是我的控制器:

 public function getView($id) 
{ 
    return view('store.show')->with('products', Product::find($id))->with('categories',Category::all())->with('db',Product::all())->with('options', Gambar::where('product_id','=',$id)->lists('name', 'img')); 
} 

正如你可以看到,在該行的末尾有代碼列表(「姓名」,「IMG」 )

它會列出列名和影像值轉換爲一個列表

一切都完美的作品接受,我需要馬可以通過選擇圖像更改下拉列表。

看待我的下拉列表中imageswap代碼:

<div class="form-group"> 

    {!! Form::open(array('url' => 'foo/bar')) !!} 
    {!! Form::label('Link Category') !!}<br /> 
    {!! Form::select('product_id',(['0' => 'Select an Option'] + $options),null,['class' => 'form-control', 'id' => 'dlist', 'onChange' => 'swapImage()']) !!} 
    {!! Form::close() !!} 

    <script type="text/javascript"> 
    function swapImage(){ 
    var image = document.getElementById("imageToSwap"); 
    var dropd = document.getElementById("dlist"); 
    image.src = dropd.value; 
    }; 
    </script> 
    </div> 

而且這個代碼在圖像上會出現選擇(產品 - >圖像是主圖像的基礎,而不是從下拉列表種子:

<img id="imageToSwap" src="{{asset($products->image)}}" /> 

在控制器作品列表的流量,但它給這個在查看源鉻:

<img id="imageToSwap" src="img/upload/20-3-291-image-1.jpg" /> 

所以圖像不會出現在頁面上,它應該出現這樣的:

<img id="imageToSwap" src="localhost:8000/img/upload/20-3-291-image-1.jpg" /> 

的問題是,如何返回「IMG」與laravel資產{{資產()}},所以圖像可以完美顯示?

回答

0

問題出在laravel和javascript之間的溝通。

正如您所指出的,您使用列表('name','img'):資產前綴將不會添加到那裏。

因此,當您執行image.src = dropd.value;時,圖像的來源沒有資產前綴。

我看到兩個可能性:

  • 要麼你做你的清單上的一些處理髮送之前: lists('name', 'img')->map(asset)(與map方法,如果是--not確保供laravel 5.0)
  • 或者你將它添加到您的javascript中:image.src = "{{asset()}}/" + dropd.value;
+0

error apply when image.src =「{{asset()}} /」+ dropd.value; helpers.php中的ErrorException第81行: 缺少資產()的參數1,在第100行中調用並在C:\ xampp \ htdocs \ myweb2 \ storage \ framework \ views \ a3ea3da5b741ea4eca39686d1e2d05b0中調用並且已定義(View:C:\ xampp \ htdocs \ myweb2 \ resources \ views \ store \ show.blade.php) –

+0

嘗試使用'asset('')' – oliverpool

+0

thanksss bro,它的工作原理 –