laravel 如何在laravel中的onclick函数上传递值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37460917/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:52:27  来源:igfitidea点击:

How to pass value on onclick function in laravel

javascriptlaravellaravel-5

提问by dhamo dharan

I want to send a value to a javascript function when a button is clicked. The value comes from the database. How can I do it using laravel blade?

我想在单击按钮时向 javascript 函数发送一个值。该值来自数据库。我怎样才能使用 Laravel Blade 做到这一点?

Here is my HTML code:

这是我的 HTML 代码:

<a  href="" onclick="add($res->driver_id);"><i class="fa fa-pencil-square-o"></i></a> 

and my javascript:

和我的 javascript:

<script>
function add(var id)
{
    alert(id);
}
</script>

回答by huuuk

Just use double curly brackets

只需使用双大括号

<a  href="" onclick="add( {{ $res->driver_id }} );"><i class="fa fa-pencil-square-o"></i></a> 

Keep in mind that when blade template renders, it replaces variavles with its values
For example if $res->driver_id = "randomstring", then after blade rendering you'll got this HTML:

请记住,当刀片模板呈现时,它会用它的值替换变量,
例如 if $res->driver_id = "randomstring",那么在刀片渲染之后你会得到这个 HTML:

<a  href="" onclick="add(randomstring);"><i class="fa fa-pencil-square-o"></i></a> 

So if you need to pass string to your add function, you need wrap argument that you passed with quotes, like so:

因此,如果您需要将字符串传递给 add 函数,则需要使用引号将传递的参数包装起来,如下所示:

<a  href="" onclick="add('{{ $res->driver_id }}');"><i class="fa fa-pencil-square-o"></i></a> 

回答by Harsh Sanghani

You have write PHP code in HTML page so it will pass as it is, You have to take it in PHP and if you got the driver_id value in your views file then you can pass it to easily to the javascript function like following code.

您已经在 HTML 页面中编写了 PHP 代码,因此它会按原样传递,您必须在 PHP 中使用它,如果您的视图文件中有 driver_id 值,那么您可以轻松地将其传递给 javascript 函数,如以下代码。

PHP Code :-

PHP 代码:-

<a  href="" onclick="add({{$res->driver_id}});"><i class="fa fa-pencil-square-o"></i></a> 

Javascript code :-

Javascript代码:-

<script>
function add(id)
{
    alert(id);
}
</script>

It may help you.

它可能会帮助你。