简体   繁体   English

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

[英]How to load a page using jquery load in laravel?

I am trying to load a page using jquery load function in laravel.我正在尝试在 Laravel 中使用 jquery 加载函数加载页面。 My code is,我的代码是,

$(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 id variable works fine.当我尝试没有id变量的加载函数时,它工作正常。 Like,喜欢,

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

How can I load the id properly ?如何正确加载id

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');
});});

You are mixing worlds.你在混合世界。

Take a look, you defined the id variable in JavaScript:看一看,你在 JavaScript 中定义了id变量:

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.您不能在 PHP 中使用 JavaScript 变量。 JavaScript runs in the browser and PHP runs on the server. JavaScript 在浏览器中运行,PHP 在服务器上运行。 What you have to do is use PHP to generate the javaScript code that initialises the variable.您需要做的是使用 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 $id variable is passed to the view via your controller.该示例代码假定$id变量通过您的控制器传递给视图。

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
});});

Just do it simply :只需简单地做:

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

Keep coding, and keep sharing your resource.继续编码,并继续共享您的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM