【问题标题】:Laravel Routing with javascript parameter带有javascript参数的Laravel路由
【发布时间】:2017-10-21 11:21:44
【问题描述】:

我有一个函数,它在用户单击时从表中返回一个参数。数据是通过 ajax 提取的,这就是我使用 javascript 的原因。

我的功能很简单:

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

我想要的是传递一个 URL,它将 id 传递给 lavavel 路由(而不是简单地检查传递的参数的警报),类似于

{{ URL('quotations/p_customerselected/{id}') }}

我试过

{{ URL('quotations/p_customerselected/') }} + id;

但是抛出了一个错误。

【问题讨论】:

  • 请提供更多代码

标签: javascript laravel-5 routing


【解决方案1】:

我以不同的(可能是不合格的!)方式解决了它。在我添加的数据表上:

{"targets": 0,
                        "data": 'id',
                        render: function(data, type, full, meta) {
                            var id = full['DT_RowId'].substring(4).trim();
                            var url = "{{ URL('quotations/p_customerselected/') }}" +"/" + id;
                            return '<a href="' + url + '"><span style="color: darkblue" title="select">' + full['DT_RowId'].substring(4).trim() +'</span></a>'
                        } 
                    }

虽然有效!

【讨论】:

    【解决方案2】:

    假设您的控制器名称是 QuotationController 并且控制器函数名称是 viewcustomer。

    在 web.php 中,给你的路由命名

    Route::post('/quotations/p_customerselected/{id}', 'QuotationController@viewcustomer')->name('quotations.p_customerselected');
    

    现在在 Javascript 中:

    <script>
     function selectItem(cust_id)
     {
       var url = '{{ route("quotations.p_customerselected", ":id") }}';
       url = url.replace(':id', cust_id);
     }
    </script>
    

    【讨论】:

      【解决方案3】:

      我不认为我理解正确,但是如果您想将参数传递给 url,您可以使用数组来绑定它们。 像这样的

      {{ url('quotations/p_customerselected}', [$id]) }}
      

      UPD

      如果你在视图中生成你的js代码,你必须在引号中插入生成的url。

      "{{ url('quotations/p_customerselected}') }}" +  id
      

      【讨论】:

      • 有问题的id来自java脚本函数的传入参数
      • 如果我理解正确,您在视图中生成您的 js 代码。不是吗?
      • 为了清楚起见,我有一个带有 ajax 负载的数据表,传入的 id 是从 onClck 事件中给出的,所以我有一个 javascript 函数来捕获它。我现在想去一个 id 传递给路由的路由。
      猜你喜欢
      • 2021-10-30
      • 2017-07-10
      • 2018-08-31
      • 2015-04-17
      • 2016-01-20
      • 2017-04-05
      • 2017-04-04
      • 2015-10-30
      • 2018-04-06
      相关资源
      最近更新 更多