簡體   English   中英

如何從URL查詢字符串獲取參數值

[英]How to get parameter value from URL query string

如何解析以下URL查詢字符串以獲取數量參數的值?

myurl /頻道/ BuyCredits?數量= 2

我嘗試了無數成功的例子:

我只是試過這個例子:

  marvmentApp.controller("channelCreditController", function($scope, $http, $compile, $location) {
    var params, test2;
    $scope.parseQueryString = function() {
      var objURL, str;
      str = window.location.search;
      objURL = {};
      str.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"), function($0, $1, $2, $3) {});
      objURL[$1] = $3;
      return;
      return objURL;
    };
    params = $scope.parseQueryString();
    test2 = params['quantity'];
    debugger;
  });

我不斷收到錯誤$ 1的定義

使用JavaScript location.search屬性。 它將返回您的URL的查詢字符串(包括“?”符號)。

如果網址是http://www.example.org/index.php?param=arg

location.search?param=arg

要刪除“?”,請使用JS replace方法:

 var url = "?param=arg"; // pretend that the variable "url" is the query string url = url.replace(/[\\?]/,""); document.getElementById('output').innerHTML = "Query string is: \\"" + url + "\\""; 
 <p id="output"></p> 

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM