[英]Pull Custom Query String Variable into Hidden Form Field Using Javascript
I am trying to pull a URL query string variable into a hidden form field to submit with an email opt-in. 我正在尝试将URL查询字符串变量拉到一个隐藏的表单字段中,以选择使用电子邮件进行提交。
The URL looks like this: http://domain.com/page/?tid=123456 URL 看起来像这样: http : //domain.com/page/?tid=123456
I am able to pull the tid query string as I require with this code: 我可以使用以下代码提取tid查询字符串:
<script>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1] || ''
);
}
But I don't know how to place getURLParameter('tid') in the hidden field. 但是我不知道如何在隐藏字段中放置getURLParameter('tid') 。
I have tried inline script but it looks like this doesn't work with Javascript. 我已经尝试了内联脚本,但看起来这不适用于Javascript。
<div id="optin">
<form id="em-optin" class="moonray-form-clearfix" action="https://forms.moon-ray.com/v2.4/form_processor.php?" onsubmit="return validateForm();" method="post" accept-charset="UTF-8" _lpchecked="1">
<div style="display: none;">
<input name="subid_198" class="moonray-form-input" type="hidden" id="mr-field-element-1" value="<script>getURLParameter('tid')</script>">
<input name="email" type="text" class="email" value="Enter Your Email:" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" tabindex="501">
<input type="submit" name="submit-button" value="Sign-up Now!" class="submit" id="mr-field-element-4" tabindex="502" "=" ">
</form>
</div>
Is anyone able to advise how I do this? 有谁能建议我该怎么做?
Hipporter. Hipporter。 Have you tried to assign the value by using the following code: 您是否尝试通过使用以下代码来分配值:
var your_var = getURLParameter(name);
document.getElementById('mr-field-element-1').value = your_var;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.