laravel如何路由到JavaScript上的路由? [英] laravel how to route to a route on a javascript?

查看:50
本文介绍了laravel如何路由到JavaScript上的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个jQuery

I have this jquery

$(".waitingTime .button").click(function () {
    alert("Ema");
});

我有一个像这样的a标记:

I have a a tag like this:

<a href="{{ URL::to('restaurants/20') }}"></a>

我可以在jquery函数中执行相同的href操作吗?

Can I do the same href action in the jquery function?

非常感谢

推荐答案

是的,这与Laravel无关. 有不同的可能性.如果您的查询嵌入在相同的laravel视图中,则可以将URL直接放在jQuery代码中,例如:

yes this is possible and has nothing to do with Laravel. There are different possibilities. If your query is embedded within the same laravel view, you put the URL directly in your jQuery code, for example like this:

$(".waitingTime .button").click(function () {
  window.location.href = "{{URL::to('restaurants/20')}}"
});

但是我认为最好的选择是在按钮标签上添加URL作为数据属性,然后让jquery转到该URL.这样,您可以使按钮更具动态性,并具有更多封装的代码.

But I think the best option is to add the URL on your button tag as a data attribute and then let jquery go to that URL. That way you can make your buttons more dynamic and have more capsulated code.

一个例子可能是:

<div class="waitingTime">
  <button class="button link-button" data-href="{{URL::to('restaurants/20')}}">
  Click me
  </button>
</div>

$(".link-button").click(function () {
  window.location.href = $(this).data('href');
});

这样,您始终可以为link-button类的按钮提供data-href属性,该属性具有单击按钮时要打开的URL,而不必添加其他jquery.

That way you can always give a button with the class link-button a data-href attribute with the URL you want to open when the button is clicked and don't have to add additional jquery.

这篇关于laravel如何路由到JavaScript上的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆