Javascript 如何获取URL中第一个查询字符串的值

Javascript 如何获取URL中第一个查询字符串的值,javascript,Javascript,Hi im使用以下代码获取表单字段中的查询字符串值: <script> var url = window.location.search; url = url.replace("?Id=", ''); // remove the ? </script> <script>document.write("<input name='Id' data-constraints='@Required(groups=[customer])' id='Id' type

Hi im使用以下代码获取表单字段中的查询字符串值:

<script>
var url = window.location.search;
url = url.replace("?Id=", ''); // remove the ? 
</script>
<script>document.write("<input name='Id' data-constraints='@Required(groups=[customer])' id='Id' type='text' value='"+url+"' />");</script>

var url=window.location.search;
url=url.replace(“?Id=“,”);//是否删除?
文件。填写(“”);

如果我访问[这不是一个链接](),它会在字段中记录Id,但当包含更多参数时,它也会在字段中记录,我只想在表单字段中添加第一个或任何特定的查询字符串参数?

基本上,您需要检查是否存在符号AND,然后将子字符串添加到符号AND

您可以在执行替换后添加此代码:

var start = url.indexOf('&');

if(start > 0) {
    url = url.substring(0, start);
}

如何利用查询字符串参数构建一个漂亮的小关联数组,例如:

var urlParams = [];
window.location.search.replace("?", "").split("&").forEach(function (e, i) {
    var p = e.split("=");
    urlParams[p[0]] = p[1];
});

// We have all the params now -> you can access it by name
console.log(urlParams["Id"]);
你用过搜索吗?可能重复的