简体   繁体   English

使用Javascript将自定义查询字符串变量提取到隐藏的表单字段中

[英]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.

相关问题 网址查询字符串中变量的javascript表单字段填充 - javascript form field population from variable in url query string 使用JavaScript发送带有表单的单个隐藏字段 - Send single hidden field with form using javascript 将Javascript变量传递给Rails form_for hidden_​​field - Passing a Javascript Variable to a Rails form_for hidden_field 使用GET提交表单时,不要在查询字符串中包含隐藏字段 - Do not include hidden field in query string when submitting form with GET 将URL变量拉入隐藏的表单值 - Pull URL variable into hidden form value 使用Jquery或Javascript将隐藏字段值设置为Javascript变量 - Set Hidden Field value to Javascript Variable using Jquery or Javascript 从查询字符串中检索值,并将其以隐藏值的形式发送-javascript - Retrieve value from query string and send it in a form as HIDDEN VALUE - javascript 使用Javascript将参数从URL传递到隐藏的表单字段(Gridview) - Using Javascript to pass a parameter from a URL to a hidden form field (Gridview) 无法使用JavaScript或jQuery设置隐藏的表单字段 - Cannot set a hidden form field using JavaScript nor with jQuery 使用表单隐藏字段将JavaScript数组发布到RoR控制器 - Post javascript array to RoR controller using a form hidden field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM