如何获取url参数并将其设置为文本框onload的默认值
Posted
技术标签:
【中文标题】如何获取url参数并将其设置为文本框onload的默认值【英文标题】:how to get the url parameters and set it as the default value of a text box onload 【发布时间】:2020-06-03 06:31:03 【问题描述】:网址
http://localhost:8082/EPHI2/SearchOrg.html?orgName=Appollo
我必须从这个url中提取参数并将其设置为页面的文本框onload的默认值。
我的HTML页面如下。
SearchOrg.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style>
.topnav
overflow: hidden;
background-color: #FCFEEA;
.topnav a
float: left;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
.topnav a:hover
background-color: #CBF894;
color: black;
.topnav a.active
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<div class="topnav">
<a href="landing.html">Home</a>
<a href="search.html">Search Patient</a>
<a href="manage_policy.html">Manage Policy</a>
<a href="#" class="xactive">Search Organization</a>
</div>
<br>
<br>
<br>
<br>
<form style="padding: 10px 400px;" name="searchOrganization" action="viewOrganization" method="post">
<input type="text" name="searchOrg" id="searchOrg" class="form-control"
placeholder="Search the organization" /> <br>          <input
type="submit" class="btn btn-primary" id="Search" value="Search" /> <input
type="button" class="btn btn-primary" id="ok" value="OK" /> <input
type="reset" class="btn btn-primary" id="clear" value="Clear" />
</form>
</body>
</html>
我必须将文本框“searchOrg”的默认值设置为url参数“orgName”的值。
【问题讨论】:
试试这个:***.com/q/19491336 【参考方案1】:这里有完整的代码,复制一下,谢谢
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style>
.topnav
overflow: hidden;
background-color: #FCFEEA;
.topnav a
float: left;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
.topnav a:hover
background-color: #CBF894;
color: black;
.topnav a.active
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<div class="topnav">
<a href="landing.html">Home</a>
<a href="search.html">Search Patient</a>
<a href="manage_policy.html">Manage Policy</a>
<a href="#" class="xactive">Search Organization</a>
</div>
<br>
<br>
<br>
<br>
<form style="padding: 10px 400px;" name="searchOrganization" action="viewOrganization" method="post">
<input type="text" name="searchOrg" id="searchOrg" class="form-control"
placeholder="Search the organization" /> <br>          <input
type="submit" class="btn btn-primary" id="Search" value="Search" /> <input
type="button" class="btn btn-primary" id="ok" value="OK" /> <input
type="reset" class="btn btn-primary" id="clear" value="Clear" />
</form>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$( window ).on( "load", function()
function getParameterByName( name,href )
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
var url = window.location.href;
$('#searchOrg').val(getParameterByName("orgName", url));
);
</script>
</body>
</html>
【讨论】:
我已经在脚本中使用 jquery 1.12.4 无论如何我们可以在 1.12.4 @Tausif 中运行此代码 只需在 上将 jquery 链接更改为 1.12.4 谢谢@Tausiif以上是关于如何获取url参数并将其设置为文本框onload的默认值的主要内容,如果未能解决你的问题,请参考以下文章