laravel 如何在laravel中使用jquery load加载页面?

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

How to load a page using jquery load in laravel?

jquerylaravelurl

提问by Vinod VT

I am trying to load a page using jquery load function in laravel. My code is,

我正在尝试在 Laravel 中使用 jquery 加载功能加载页面。我的代码是,

$(document).ready(function() {
    $('.btn').on('click', function(){
        id = this.id;
        $('#div-details').load('{{URL::to("users/details/'+id+'/view")}}');
    });
});

When I try this code I am getting an error,

当我尝试此代码时,出现错误,

syntax error, unexpected ''+id+'' (T_CONSTANT_ENCAPSED_STRING)

When I try the load function without idvariable works fine. Like,

当我尝试不带id变量的加载函数时工作正常。喜欢,

 $('#div-details').load('{{URL::to("users/details/55/view")}}');

How can I load the idproperly ?

我怎样才能id正确加载?

采纳答案by Vinod VT

It will work perfectly when I use like this,

当我这样使用时它会完美运行,

$(document).ready(function() {
$('.btn').on('click', function(){
    var id = $(this).attr('id');
    $('#div-details').load('{{URL::to("users/details/")}}/'+id+'/view');  // Load like this
});});

回答by CDF

Just pass it as a legal path, check the code below

只需将其作为合法路径传递,检查下面的代码

$(document).ready(function() {
$('.btn').on('click', function(){
    var id = $(this).attr('id');
    $('#div-details').load('users/details/'+id+'/view');
});});

回答by Javi Stolz

You are mixing worlds.

你在混合世界。

Take a look, you defined the idvariable in JavaScript:

看一看,你id在 JavaScript 中定义了变量:

id = this.id;

But then later you use it in PHP

但后来你在 PHP 中使用它

{{URL::to("users/details/'+id+'/view")}}

You cannot use a JavaScript variable in PHP. JavaScript runs in the browser and PHP runs on the server. What you have to do is use PHP to generate the javaScript code that initialises the variable.

您不能在 PHP 中使用 JavaScript 变量。JavaScript 在浏览器中运行,PHP 在服务器上运行。您需要做的是使用 PHP 生成初始化变量的 javaScript 代码。

$(document).ready(function() {
    $('.btn').on('click', function(){
        id = '{{$i}}';
        $('#div-details').load('{{URL::to("users/details/'+id+'/view")}}');
    });
});

That sample code assumes the $idvariable is passed to the view via your controller.

该示例代码假定$id变量是通过控制器传递给视图的。