Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery和JavaScript获取URL的特定部分?_Javascript_Php_Jquery_Html_Wordpress - Fatal编程技术网

如何使用jQuery和JavaScript获取URL的特定部分?

如何使用jQuery和JavaScript获取URL的特定部分?,javascript,php,jquery,html,wordpress,Javascript,Php,Jquery,Html,Wordpress,我正在通过jQueryAjax在WordPress中查看订单。成功发布后,WordPress会向我返回一个响应,结果为success值和url值 我想得到这个url的特定部分,这样我就可以使用id作为我的目标对象 这是url的结构: 这是我当前的代码: j('.my-checkout').on('submit', function(evt) { evt.preventDefault(); var billing_customer_type = j("#billing_customer_

我正在通过jQueryAjax在WordPress中查看订单。成功发布后,WordPress会向我返回一个响应,结果为
success
值和
url

我想得到这个
url
的特定部分,这样我就可以使用
id
作为我的目标对象

这是url的结构:

这是我当前的代码:

j('.my-checkout').on('submit', function(evt) {
  evt.preventDefault();

  var billing_customer_type = j("#billing_customer_type").val();
  // and so on...

  j.ajax({
    type: 'POST',
    url: 'http://localhost/mywebsite/ajax=checkout',
    cache: false,
    data: {
      'billing_customer_type': billing_customer_type,
      // and so on..
    },
    success: function(result) {
      var orderResponseUrl = result.redirect;

      j('.order-response-url').html(orderResponseUrl);
      // http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459

      orderResponseUrl.split("/");
      console.log(orderResponseUrl[3]);
    },
    error: function(xhr, status, error) {
      console.log(error);
    },
    complete: function() {}
  });
});
我上面代码的结果就是字母“p”。我想是因为它是从http的第一个字母开始的,我使用了索引
[3]


您知道如何获取
url
的特定部分,即
28564

因为当您执行
orderResponseUrl.split(“/”)时它不会更改orderResponseUrl,而是创建一个新数组

var parts = orderResponseUrl.split("/");
console.log(parts);

因为当您执行
orderResponseUrl.split(“/”)时它不会更改orderResponseUrl,而是创建一个新数组

var parts = orderResponseUrl.split("/");
console.log(parts);

如果长度始终相同,则可以使用子字符串函数

var str = "Hello world!";
var res = str.substring(1, 4);
现在res包含

console.log(res); // ell
如果你不知道索引,你可以这样找到

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
现在看起来像

console.log(n); // 13

如果长度始终相同,则可以使用子字符串函数

var str = "Hello world!";
var res = str.substring(1, 4);
现在res包含

console.log(res); // ell
如果你不知道索引,你可以这样找到

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
现在看起来像

console.log(n); // 13

如果需要与之交互的URL总是相同的,则可以在“order received/”部分拆分返回的URL(该部分给出了之前所有内容和之后所有内容的数组)

然后,您可以使用
parseFloat()
来获取订单号,而不是在“?”上再次拆分,这是另一种方式。这是因为parseFloat返回的所有数值不超过但不包括第一个非数字字符(“?”)

var-urlStr='1〕http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459';
var orderNumber=parseFloat(urlStr.split('order-received/')[1]);

console.log(orderNumber);//给出28564
如果您需要与之交互的URL总是相同的,您可以在“order received/”部分拆分返回的URL(该部分给出了之前的所有内容以及之后的所有内容的数组)

然后,您可以使用
parseFloat()。这是因为parseFloat返回的所有数值不超过但不包括第一个非数字字符(“?”)

var-urlStr='1〕http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459';
var orderNumber=parseFloat(urlStr.split('order-received/')[1]);
console.log(订单号)//给出28564