简体   繁体   English

如何在JavaScript中的URL中传递参数

[英]How can I get the param passed in an URL in JavaScript

I have some datatables and I want to get the param id 194 out of the URL 我有一些数据表,我想从URL中获取参数ID 194

admin/customer-management/updater/id/194

My source AJAX: 我的来源AJAX:

"sAjaxSource": "/<?= Zend_Registry::get('Zend_Locale')->getLanguage(); ?>/admin/CustomerManagement/getlistcontactsajax/idCustomer/" + $(this).attr('value');

I do this + $(this).attr('value'); 我这样做+ $(this).attr('value'); but it does not work. 但它不起作用。

Visibly, you use PHP (with Zend) to write your js code. 显然,您使用PHP(与Zend一起)编写了js代码。
You can recover the id in your controller with : 您可以使用以下命令在控制器中恢复ID:

$id = $this->getRequest()->getParam('id');

So you can write your js code with it (may be pass variable in the view): 因此,您可以使用它来编写js代码(可能是视图中的pass变量):

"sAjaxSource": "/<?=Zend_Registry::get('Zend_Locale')->getLanguage();?>/admin/CustomerManagement/getlistcontactsajax/idCustomer/<?= $id?>",

如果您拥有url变量,则可以使用正则表达式url.match(/\\d+$/)来获取它

you could do this by: 您可以通过以下方式做到这一点:

parseInt(location.href.split("/").pop())

or 要么

parseInt(location.href.replace(/[^0-9]/ig, ""))

This is what I came up with, given the unclear question. 鉴于不清楚的问题,这就是我想出的。

location.href will give you the current page URL. location.href将为您提供当前页面的URL。 In your case (again, my assumption), it's admin/customer-management/updater/id/194 就您而言(同样,我的假设)是admin/customer-management/updater/id/194

Hence, do the following. 因此,请执行以下操作。

var ID = location.href.split("/").pop();

The above creates an array, like below and then assigns the last item in the array to the variable ID 上面创建了一个数组,如下所示,然后将数组中的最后一项分配给变量ID

["admin", "customer-management", "updater", "id", "194"]

You can then do: 然后,您可以执行以下操作:

"sAjaxSource": "/<?=Zend_Registry::get('Zend_Locale')->getLanguage();?>/admin/CustomerManagement/getlistcontactsajax/idCustomer/" + ID

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

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