我有一个简单的表单,我想拉一个url参数并用该值填充其中一个隐藏字段。
例如,网址是www.myurltest.html?placement = xyz
这是我的表格代码与js。当我加载页面时,值xyz没有被填充到隐藏字段中。我检查的方式是在页面刷新之后,我检查源代码以查看值是否在html中。请让我知道什么是错的,我也不是贸易开发商,所以这是一个额外的挑战。
<script>
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>
<form method="post" action="http://xyz.com" >
<ul><li ><label>First Name:</label><input name="firstName" id="firstName" type='text' value="" maxlength='255' tabIndex='1' ></li>
<li ><label>Last Name:</label><input name="lastName" id="lastName" type='text' value="" maxlength='255' tabIndex='2' /></li>
<li ><label>Email:</label><input name="email" id="email" type='text' value="" maxlength='255' tabIndex='3' /></li>
<li ><label>Company Name:</label><input name="name" id="name" type='text' value="" maxlength='255' tabIndex='4' /></li>
<li>
<input type='submit' foo=en_US value='testtest' name='submit' />
</li>
</ul>
<input type="hidden" name="placement" id="placement" value="" />
<script type="text/javascript">
document.getElementById("placement").value = getParameterByName("placement");
</script>
</form>
答案 0 :(得分:0)
该值不会出现在源中。如果您使用Chrome F12开发工具,则可以检查该值。它似乎对我有用。
<html>
<head>
<script type="text/javascript">
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>
</head>
<body>
<form method="post" action="http://xyz.com" >
<ul>
<li><label>First Name:</label><input name="firstName" id="firstName" type='text' value="" maxlength='255' tabIndex='1' ></li>
<li><label>Last Name:</label><input name="lastName" id="lastName" type='text' value="" maxlength='255' tabIndex='2' /></li>
<li><label>Email:</label><input name="email" id="email" type='text' value="" maxlength='255' tabIndex='3' /></li>
<li><label>Company Name:</label><input name="name" id="name" type='text' value="" maxlength='255' tabIndex='4' /></li>
<li><input type='submit' foo=en_US value='testtest' name='submit' /></li>
</ul>
<input type="hidden" name="placement" id="placement" value="" />
<script type="text/javascript">
document.getElementById("placement").value = getParameterByName("placement");
</script>
</form>
</body>
</html>