从url()获取数据

时间:2017-03-30 05:33:56

标签: jquery

我正在尝试从此网址()获取idval并将其分配到文本框中http://localhost:59113/Project/ProjectAllocation/?id=9%20&val=%20dgds 我尝试了这段代码,但得到的文本框值如下val=%20dgds,%20dgds

  var url = document.URL;
var id = /id=([^&]+)/.exec(url);
var val = /val=([^&]+)/.exec(url);
var result = id ? id : ' ';
var result = getId()
 $('.currentProjectName').val(val)

2 个答案:

答案 0 :(得分:1)

您需要解码URI



var url = 'http://localhost:59113/Project/ProjectAllocation/?id=9%20&val=%20dgds;'
var id = decodeURI(/id=([^&]+)/.exec(url)[1]);
var val = decodeURI(/val=([^&]+)/.exec(url)[1]);
$('#txtid').val(id);
$('#txtval').val(val);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="txtid" value="">
<input type="text" id="txtval" value="">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我从这个

获得了解决方案
function a() {debugger
    var url = document.URL;
    var param = getUrlParam("id");
    var param = getUrlParam("val");
    $('.currentProjectName').val(param)
}

function getUrlParam(param) {
    param = param.replace(/([\[\](){}*?+^$.\\|])/g, "\\$1");
    var regex = new RegExp("[?&]" + param + "=([^&#]*)");
    var url = decodeURIComponent(window.location.href);
    var match = regex.exec(url);
    return match ? match[1] : "";
}