JavaScript UTM参数数组未填充表单字段。

huangapple go评论64阅读模式
英文:

JavaScript UTM parameters array not populating form fields

问题

以下是您提供的代码的中文翻译部分:

我有以下脚本,该脚本从URL查询字符串中获取UTM参数值,即 'example.com?utm_campaign=testCampaign&utm_source=testSource',并应该填充隐藏表单字段。然而,脚本似乎不起作用,因为输入字段的值为空?

<!-- 开始代码片段js 隐藏false 控制台true Babelfalse -->

<!-- 语言: lang-js -->
locationSearch = 'example.com?utm_campaign=testCampaign&amp;utm_source=testSource';
function getUtmParams(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&amp;]" + name + "=([^&#]*)"),
        results = regex.exec(locationSearch);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var utmArray = ['utm_campaign', 'utm_medium', 'utm_source', 'utm_content', 'utm_term'];

for (var i = 0; i < utmArray.length; i++) {
    console.log("a " + utmArray[i] + " = " + getUtmParams(utmArray[i]));
    if (getUtmParams(utmArray[i])) {
        document.querySelector("div." + utmArray[i] + " input[type=hidden]").value = getUtmParams(utmArray[i]);
    }
}
<!-- 语言: lang-html -->
<div class="utm_campaign">
    <input type="hidden" name="2302121" id="e2922" value="">
</div>

<div class="utm_source">
    <input type="hidden" name="2388234" id="d4033" value="">
</div>
<!-- 结束代码片段 -->

以下这行代码似乎未填充输入字段,因为值保持为空:

document.querySelector("div." + utmArray[i] + " input[type=hidden]").value = getUtmParams(utmArray[i]);

英文:

I have the below script which takes UTM parameter values from the URL querystring i.e. &#39;example.com?utm_campaign=testCampaign&amp;utm_source=testSource&#39; and is supposed to populate hidden form fields. However the script doesn't seem to work as the input field values are empty?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

locationSearch=&#39;example.com?utm_campaign=testCampaign&amp;utm_source=testSource&#39;;
function getUtmParams(name) {
    name = name.replace(/[\[]/, &quot;\\[&quot;).replace(/[\]]/, &quot;\\]&quot;);
    var regex = new RegExp(&quot;[\\?&amp;]&quot; + name + &quot;=([^&amp;#]*)&quot;),
    results = regex.exec(locationSearch);
    return results === null ? &quot;&quot; : decodeURIComponent(results[1].replace(/\+/g, &quot; &quot;));
}

var utmArray = [&#39;utm_campaign&#39;,&#39;utm_medium&#39;,&#39;utm_source&#39;,&#39;utm_content&#39;,&#39;utm_term&#39;];

for (var i = 0; i &lt; utmArray.length; i++) {
  console.log(&quot;a &quot; + utmArray[i] + &quot; = &quot; + getUtmParams(utmArray[i]));
  if(getUtmParams(utmArray[i])){
document.querySelector(&quot;div.&quot; + utmArray[i] + &quot; input[type=hidden]&quot;).value = getUtmParams(utmArray[i]);
  }
}

<!-- language: lang-html -->

&lt;div class=&quot;utm_campaign&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;2302121&quot; id=&quot;e2922&quot; value=&quot;&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;utm_source&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;2388234&quot; id=&quot;d4033&quot; value=&quot;&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

The folowing line seems to not populate the input fields as the values remain empty?

document.querySelector(&quot;div.&quot; + utmArray[i] + &quot; input[type=hidden]&quot;).value = getUtmParams(utmArray[i]);

答案1

得分: 1

你的 input 字段仍然是隐藏的。要查看它们,你需要更改 type 属性。

locationSearch = 'example.com?utm_campaign=testCampaign&utm_source=testSource';

function getUtmParams(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(locationSearch);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var utmArray = ['utm_campaign','utm_medium','utm_source','utm_content','utm_term'];

for (var i = 0; i < utmArray.length; i++) {
  console.log("a " + utmArray[i] + " = " + getUtmParams(utmArray[i]));
  if(getUtmParams(utmArray[i])){
    document.querySelector("div." + utmArray[i] + " input[type=hidden]").value = getUtmParams(utmArray[i]);
    document.querySelector("div." + utmArray[i] + " input[type=hidden]").type = 'text';
  }
}
<div class="utm_campaign">
  <input type="hidden" name="2302121" id="e2922" value="">
</div>

<div class="utm_source">
  <input type="hidden" name="2388234" id="d4033" value="">
</div>
英文:

Your input fields are still hidden. To see them, you need to change the type attribute.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

locationSearch=&#39;example.com?utm_campaign=testCampaign&amp;utm_source=testSource&#39;;

function getUtmParams(name) {
    name = name.replace(/[\[]/, &quot;\\[&quot;).replace(/[\]]/, &quot;\\]&quot;);
    var regex = new RegExp(&quot;[\\?&amp;]&quot; + name + &quot;=([^&amp;#]*)&quot;),
    results = regex.exec(locationSearch);
    return results === null ? &quot;&quot; : decodeURIComponent(results[1].replace(/\+/g, &quot; &quot;));
}

var utmArray = [&#39;utm_campaign&#39;,&#39;utm_medium&#39;,&#39;utm_source&#39;,&#39;utm_content&#39;,&#39;utm_term&#39;];

for (var i = 0; i &lt; utmArray.length; i++) {
  console.log(&quot;a &quot; + utmArray[i] + &quot; = &quot; + getUtmParams(utmArray[i]));
  if(getUtmParams(utmArray[i])){
document.querySelector(&quot;div.&quot; + utmArray[i] + &quot; input[type=hidden]&quot;).value = getUtmParams(utmArray[i]);
document.querySelector(&quot;div.&quot; + utmArray[i] + &quot; input[type=hidden]&quot;).type = &#39;text&#39;;
  }
}

<!-- language: lang-html -->

&lt;div class=&quot;utm_campaign&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;2302121&quot; id=&quot;e2922&quot; value=&quot;&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;utm_source&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;2388234&quot; id=&quot;d4033&quot; value=&quot;&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 17:24:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定