laravel 如何将 PHP 变量从 Blade 格式的 HTML 传递给 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28937200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass PHP variable to JavaScript function from Blade format HTML
提问by Muhammad Raza
I am using Laravel Blade format for my HTML and I am passing multiple PHP variables to JavaScript functions. The issue is that the variables being passed to JavaScript function could have special characters.
我正在为我的 HTML 使用 Laravel Blade 格式,并且我正在将多个 PHP 变量传递给 JavaScript 函数。问题是传递给 JavaScript 函数的变量可能具有特殊字符。
I want the JavaScript function to be as follows:
我希望 JavaScript 函数如下:
jsFunction("argument1","argument2","argument3")
What I get:
我得到的:
jsFunction('argument1','argument2','argument3')
My Blade format code is:
我的刀片格式代码是:
<a href="#" onclick="displayBannerInvoice('{{$bannerProperty->property_id}}','{{ $bannerProperty->id }}','{{$bannerProperty->end_date}}','{{$bannerProperty->no_of_days}}','{{$bannerProperty->total}}','{{$bannerProperty->vat_percentage}}')"></a>
回答by Kyslik
The best shot is to not reinvent the wheel and try searching :)
最好的办法是不要重新发明轮子并尝试搜索:)
There is a package already on github https://github.com/laracasts/PHP-Vars-To-Js-Transformer
github 上已经有一个包https://github.com/laracasts/PHP-Vars-To-Js-Transformer
It does exactly what you want. Its for Laravel 5 as well as Laravel 4.
它完全符合您的要求。它适用于 Laravel 5 和 Laravel 4。
回答by Vladimir verleg
You could use the following syntax to display a php variable in laravel blade:
您可以使用以下语法在 laravel 刀片中显示 php 变量:
{!! $someVar !!}
example:
例子:
console.log( {!! $someVar !!} );
回答by KalC
Here is an example of how I am doing it. I have a form with an update button which calls a JavaScript function.
这是我如何做的一个例子。我有一个带有调用 JavaScript 函数的更新按钮的表单。
{!! Form::button('Update', array('class' => 'btn btn-success', 'onClick' => 'updateTeamName(' . $team_id . ')')) !!}
I am passing the $team_id variable (passed to the view by the controller method) to the JavaScript function.
我将 $team_id 变量(通过控制器方法传递给视图)传递给 JavaScript 函数。
Hope this helps.
希望这可以帮助。
回答by mahmood morid
I've tried many syntax and finally this solved the problem. This is your answer:
我尝试了很多语法,最后这解决了问题。这是你的答案:
jsFunction({{"$var1"}}{{","}}{{"var2"}})