Php laravel如何在javascript上路由到路由?
我有这个jqueryPhp laravel如何在javascript上路由到路由?,php,laravel,laravel-4,blade,Php,Laravel,Laravel 4,Blade,我有这个jquery $(".waitingTime .button").click(function () { alert("Ema"); }); 我有一个a标签,如下所示: <a href="{{ URL::to('restaurants/20') }}"></a> $(".waitingTime .button").click(function () { window.location.href = "{{URL::to('restaurants/2
$(".waitingTime .button").click(function () {
alert("Ema");
});
我有一个a
标签,如下所示:
<a href="{{ URL::to('restaurants/20') }}"></a>
$(".waitingTime .button").click(function () {
window.location.href = "{{URL::to('restaurants/20')}}"
});
我可以在jquery函数中执行相同的href
操作吗
非常感谢只需在javascript中输出php即可
$(".waitingTime .button").click(function () {
window.location.href = "<?php echo URL::to('restaurants/20'); ?>";
});
$(“.waitingTime.button”)。单击(函数(){
window.location.href=“”;
});
是的,这是可能的,与Laravel无关。
有不同的可能性。如果您的查询嵌入在同一个laravel视图中,则可以将URL直接放入jQuery代码中,例如:
<a href="{{ URL::to('restaurants/20') }}"></a>
$(".waitingTime .button").click(function () {
window.location.href = "{{URL::to('restaurants/20')}}"
});
但我认为最好的选择是在按钮标记上添加URL作为数据属性,然后让jquery转到该URL。这样,您可以使您的按钮更加动态,并有更多的封装代码
一个例子可能是:
<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按钮”)。单击(函数(){
window.location.href=$(this.data('href');
});
这样,您就可以始终使用class
link button
adata href
属性提供一个按钮,该按钮带有单击按钮时要打开的URL,而不必添加额外的jquery。如果需要,来自js的变量可以使用此属性
if(data.cookies == 0){
location.replace("{{ route('delivery') }}?id="+id);
}
相同的href action是什么意思?@RahilWazir我的意思是,当我点击
a
标签时,浏览器会转到href url。我希望在javascript函数中具有相同的功能。但这里是拉威尔,也许是不同的?我不知道你应该使用laravel的Syntax:{{}
。就我个人而言,我不喜欢这个答案,用javascript编写的php,它相当普通