2017-04-16 39 views
-1

我有一個選擇選項,如下所示。不能在選擇的onChange函數中通過葉片變量

@foreach($destinations as $destination) 
{!! Form::select('destination', $counts, $destination->ordering, array('onchange'=>'fetch_select(this.value,$destination->id)') !!} 
@endforeach 

我想在上面的onchange函數中傳遞一個名爲$ destination-> id的變量,這將在後面的java-script中觸發一個函數。但是,我無法像上面所做的那樣通過這個變量。引發了很多錯誤。它說

Uncaught SyntaxError: Unexpected token > 

在檢查控制檯。

我試着用:

'onchange'=>'fetch_select(this.value,destination->id)' 
'onchange'=>'fetch_select(this.value,"$destination->id")' 
'onchange'=>'fetch_select(this.value,(destination->id))' 
'onchange'=>'fetch_select(this.value,($destination->id))' 

他們沒有工作。我怎樣才能解決這個搞定。此致敬禮

回答

0

由於單引號,您的代碼正在處理destination-> id作爲文字。你實際上獲得值destination-> id作爲參數傳遞給函數,而destination-> id是不合法的javascript。它讀取它作爲目的地減去大於ID,這是沒有意義的。

嘗試

'fetch_select(this.value,' . $destination->id . ')' 
0

形式選擇功能沒有關閉,丟失),試試下面的代碼,以避免語法錯誤

{!! Form::select('destination', $counts, $destination->ordering, array('onchange'=>"fetch_select(this.value,$destination->id)")) !!}