有没有办法将PHP对象作为JavaScript参数传递给HTML?

时间:2015-09-21 07:57:33

标签: javascript php html laravel-5

我使用Laravel 5进行项目。我从视图中显示表格,我想在点击此链接时传递它是数据值所有数据到JavaScript函数。我尝试了几种方法,但不能这样做。

  @foreach ($basl_officers as $basl_officer)
                        <tr>
                            <td><a href="#"   onClick="functionOne($basl_officer)"  >{{ $basl_officer->officerName }} </a></td>        
                            <td align='center'>
                                {!! Form::open(['method' => 'DELETE', 'route'=>['basl_officers_page.destroy',$basl_officer->id]]) !!}
                                <a href="{{route('basl_officers_page.edit',$basl_officer->id)}}" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-pencil"></span> </a> &nbsp &nbsp
                                <button type="submit" class="btn btn-default btn-sm" onclick="return confirm('Are you sure?')"> <span class="glyphicon glyphicon-trash"></span> </button> 
                                {!! Form::close() !!}
                            </td>
                        </tr>
 @endforeach

JavaScript代码

<script type="text/javascript">
function functionOne(x) {
    alert('You clicked the top text' + x);
}
function functionTwo() {
    alert('You clicked the bottom text');
}

2 个答案:

答案 0 :(得分:4)

你可以试试这个:

onClick="functionOne({{ json_encode($basl_officer) }})"

答案 1 :(得分:1)

我在.blade文件中使用此代码工作了。

<script>
    var object = <?php echo json_encode($object) ?>;
    console.log(object);
</script>

这是假设通过控制器提供对象。