【问题标题】:Getting a number out of the url [duplicate]从网址中获取数字[重复]
【发布时间】:2015-05-27 12:14:23
【问题描述】:

我需要从 url 中获取 id。 所以如果网址是这样的:http://localhost:17241/Chart.aspx?id=11

我应该得到数字 11 作为输出。 但是每个站点都有不同的 id。之后,我将使用它设置 z-index。

我已经尝试写一些东西了

$.urlParam = function (name) {
var patt1 = new RegExp('[^17241]').exec(window.location.href);
return result[1] || 0;
document.getElementById("link").innerHTML = result;}

但这根本不起作用。 有谁知道该怎么做?

所以现在它应该改变 z-index:

var url = window.location.href;
var params = url.split('?');
var id = params[1].split('=')[1]; //params[1] will contain everything after ? 
console.log(id);
if(id == 11)
{
    $(“#one”).each(function() {
        $(this).css(“z-index“, 0);
    });

}

else if(id == 31)
{
    $(“#four”).each(function() {
        $(this).css(“z-index“, 0);
    });
}

它应该改变 div 的 z-index

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<div class="contentwidth" id="chartdiv">
    <asp:Label ID="lblStatus" runat="server"></asp:Label>
    <div id="one" style="position: absolute; top: 0; left: 0; height: 100%; width: 100%;">
        <asp:Literal ID="ltrRenderChart" runat="server"></asp:Literal>
    </div>
    <div id="two" style="position: absolute; top: 0; left: 50%; height: 100%; width: 100%; z-index:-1">
        <asp:Literal ID="ltrRenderChart2" runat="server"></asp:Literal>
    </div>
    <div id="three" style="position: absolute; top: 50%; left: 0; height: 100%; width: 100%; z-index:-1">
        <asp:Literal ID="ltrRenderChart3" runat="server"></asp:Literal>
    </div>
    <div id="four" style="position: absolute; top: 50%; left: 50%; height: 100%; width: 100%; z-index:-1 ">
        <asp:Literal ID="ltrRenderChart4" runat="server"></asp:Literal>
    </div>
</div>

谢谢

【问题讨论】:

标签: javascript jquery regex


【解决方案1】:

您可以使用split()

var url = 'http://localhost:17241/Chart.aspx?id=11'
var params = url.split('?');
var id=params[1].split('=')[1]; //params[1] will contain everything after ? 
console.log(id);

编辑

要获取var url 中的网址,请将第一行替换为

var url = window.location.href;

【讨论】:

  • 我如何获取变量url中的url?
  • @fusionchart_help。查看更新。
【解决方案2】:

叫做查询字符串

这里是函数,

 function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
 }

你怎么称呼

getParameterByName(name)

以上代码来自这里 How can I get query string values in JavaScript?

【讨论】:

    【解决方案3】:

    如果你想要它作为一个实际的数字,我会写一个通用版本

    function getParamAsNumber(url, param) {
        param = param + '=';
        if (url.indexOf(param) !== -1) {
            return parseInt(url.substr(url.indexOf(param) + param.length));
        }
    }
    

    它将参数+'='(在你的情况下为'id=')之后的字符串转换为整数

    所以你可以这样做

    getParamAsNumber(window.location.href, 'id');
    

    【讨论】:

      【解决方案4】:

      我正在使用这种方法从当前窗口的URL中获取任意参数:

      function obtainParameter(key) {
          var result=null, tmp;
          window.location.search //this attribute stores the string found after a ‘?’ in the URL, including the question mark.
              .substr(1) //removing question mark in position 0
              .split("&") //obtaining the pairs key=value
              .forEach(function (item) {
                  tmp = item.split("="); // tmp[0] is the parameter name, tmp[1] is the value
                  if (tmp[0] === key) result = decodeURIComponent(tmp[1]);
              });
          return result;
      }
      

      有了它,你只需要写:

      var id=obtainParameter('id');
      

      【讨论】:

        【解决方案5】:
        var url = "http://localhost:17241/Chart.aspx?id=11";
        
        url = url.split("=")[1];
        

        【讨论】:

          【解决方案6】:
          var url = 'http://localhost:17241/Chart.aspx?id=11';
          var match = url.match(/id=(\d+)/)
          if (match) {
              var id = match[1];
          }
          

          【讨论】:

          • 但是如何获取变量 var 中的 url?
          • @Zee 已经解释过了。
          【解决方案7】:

          这个小任务不需要正则表达式。 您可以使用以下功能来获取您的要求:

          function getvalue(murl,para)
          {
             try
             {
                  return (murl.split(para+"=")[1]).split("&")[0];
             }
          catch(e)
             {
                return '';
             }
          }
          var murl = 'http://localhost:17241/Chart.aspx?id=11';
          var id = getvalue(murl,'id');
          

          当没有找到 GET 参数时,它不会返回任何内容。

          在你的情况下:

          var id = getvalue(window.location.href,'id');
          

          【讨论】:

            【解决方案8】:

            你可以使用这个函数来通过 JS 获取查询字符串参数:

            function getQueryVariable(variable) {
              var query = window.location.search.substring(1);
              var vars = query.split('&');
              for(var i=0; i<vars.length; i++) {
                var pair = vars[i].split('=');
                if(pair[0] == variable) {
                  return pair[1];
                };
              }:
              return(false);
            };
            

            然后你可以像这样得到id的值:

            var yourID = getQueryVariable('id');
            

            【讨论】:

              【解决方案9】:

              你可以使用regex:

              alert(window.location.href.match(/\?id=(\d+)/i)[1]);
              
              1. \?:匹配?。需要通过\逃跑
              2. id=:匹配精确字符串id=
              3. \d+:匹配任何号码。位数
              4. ():分组。
              5. i:不区分大小写匹配

              【讨论】:

              • 但是当 url 应该从上面读取时我该怎么做呢?
              • @fusionchart_help 您可以使用window.location.href。检查更新的答案
              【解决方案10】:

              您可以使用拆分功能:string.split(separator,limit)

               var str = "http://localhost:17241/Chart.aspx?id=11";
               var res = str.split("=", 2);
               document.getElementById("link").innerHTML = res[1];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-11-01
                • 1970-01-01
                • 2021-12-22
                • 2018-01-30
                • 2018-09-13
                • 2017-01-16
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多