2017-04-16 48 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)")) !!}