Jquery to get parameter Id from MVC url?

2019-05-06 18:33发布

I have the following url : http://www.xzy.com/AMS/Audit/Agency/10017397

I need to strip out the id: 10017397 from this url?

I tried:

  function GetURLParameter(sParam) {
            var sPageURL = window.location.search.substring(1);
            var sURLVariables = sPageURL.split('&');
            for (var i = 0; i < sURLVariables.length; i++)
            {
                var sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] == sParam)
                {
                    return sParameterName[1];
                }
            }
        }

3条回答
ゆ 、 Hurt°
2楼-- · 2019-05-06 19:03

here you go it extracts only the numbers from the string

var thestring="http://www.xzy.com/AMS/Audit/Agency/10017397";
your_required_no= thestring.match(/\d+\.?\d*/g)
alert(your_required_no)

If you are sure your url will have only the numbers after the last / then try above one

查看更多
做自己的国王
3楼-- · 2019-05-06 19:11

You can try using this regular expression to get the last number off a string: http://jsfiddle.net/2AKDQ/2/

var URL = "http://www.xzy.com/AMS/Audit/Agency/10017397";
var number = URL.match(/(\d+)$/g);

But you'll really need to provide more information.. Is the numeric value always at the end of the string? Or if not, is it always at position 4? Is the string always the same apart from the number?

查看更多
Viruses.
4楼-- · 2019-05-06 19:16

This will help you see demo

function GetURLParameter() {
                var sPageURL = window.location.href;
                var indexOfLastSlash = sPageURL.lastIndexOf("/");

                if(indexOfLastSlash>0 && sPageURL.length-1!=indexOfLastSlash)
                    return sPageURL.substring(indexOfLastSlash+1);
                else 
                   return 0;
            }
查看更多
登录 后发表回答