英文:
JavaScript UTM parameters array not populating form fields
问题
以下是您提供的代码的中文翻译部分:
我有以下脚本,该脚本从URL查询字符串中获取UTM参数值,即 'example.com?utm_campaign=testCampaign&utm_source=testSource'
,并应该填充隐藏表单字段。然而,脚本似乎不起作用,因为输入字段的值为空?
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言: lang-js -->
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]);
}
}
<!-- 语言: 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. 'example.com?utm_campaign=testCampaign&utm_source=testSource'
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='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]);
}
}
<!-- language: 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>
<!-- end snippet -->
The folowing line seems to not populate the input fields as the values remain empty?
document.querySelector("div." + utmArray[i] + " input[type=hidden]").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='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';
}
}
<!-- language: 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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论