我正在使用js.cookie.js在读取URL参数后设置cookie值。接下来,我想用cookie值填充许多隐藏字段。读取URL值并设置cookie正在运行,但在隐藏字段中设置值无效。
这是脚本:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="http://example.com/scripts/js.cookie.js" type="text/javascript"></script>
<script>
// Parse the URL
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var visitor_id = getParameterByName('utm_visitor_id');
var medium = getParameterByName('utm_medium');
var source = getParameterByName('utm_source');
var campaign = getParameterByName('utm_campaign');
var content = getParameterByName('utm_content');
var term = getParameterByName('utm_term');
var count_of_sessions = getParameterByName('utm_count_of_sessions');
var count_of_page_views = getParameterByName('utm_count_of_page_views');
// Set the cookies
Cookies.set('utm_visitor_id', visitor_id, { expires: 90 });
Cookies.set('utm_medium', medium, { expires: 90 });
Cookies.set('utm_source', source, { expires: 90 });
Cookies.set('utm_campaign', campaign, { expires: 90 });
Cookies.set('utm_content', content, { expires: 90 });
Cookies.set('utm_term', term, { expires: 90 });
Cookies.set('utm_count_of_sessions', count_of_sessions, { expires: 90 });
Cookies.set('utm_count_of_page_views', count_of_page_views, { expires: 90 });
// Grab the cookie value and set the form field values
$(document).ready(function(){
$('input[name=00N4100000NglQ2').val(utm_visitor_id);
$('input[name=00N4100000NgleO').val(utm_medium);
$('input[name=00N4100000NglpC').val(utm_source);
$('input[name=00N4100000NglyY').val(utm_campaign);
$('input[name=00N4100000NgmBh').val(utm_content);
$('input[name=00N4100000NgmIs').val(utm_term);
$('input[name=00N4100000NgmQw').val(utm_count_of_sessions);
$('input[name=00N4100000NgmZ5').val(utm_count_of_page_views);
});
</script>
以下是表单中隐藏的字段,其中id =“contact-form”
<!-- Visitor ID: -->
<input id="00N4100000NglQ2" maxlength="255" name="00N4100000NglQ2" size="20" type="hidden" value="" />
<!-- Medium: -->
<input id="00N4100000NgleO" maxlength="255" name="00N4100000NgleO" size="20" type="hidden" value="" />
<!-- Source: -->
<input id="00N4100000NglpC" maxlength="255" name="00N4100000NglpC" size="20" type="hidden" value="" />
<!-- Campaign: -->
<input id="00N4100000NglyY" maxlength="255" name="00N4100000NglyY" size="20" type="hidden" value="" />
<!-- Content: -->
<input id="00N4100000NgmBh" maxlength="255" name="00N4100000NgmBh" size="20" type="hidden" value="" />
<!-- Term: -->
<input id="00N4100000NgmIs" maxlength="255" name="00N4100000NgmIs" size="20" type="hidden" value="" />
<!-- Count of Sessions: -->
<input id="00N4100000NgmQw" maxlength="255" name="00N4100000NgmQw" size="20" type="hidden" value="" />
<!-- Count of Pageviews: -->
<input id="00N4100000NgmZ5" maxlength="255" name="00N4100000NgmZ5" size="20" type="hidden" value="" />
我想知道隐藏字段名称是否存在问题,例如“00N4100000NgmZ5”,它们是来自salesforce.com的自定义字段名称
感谢您的帮助。
答案 0 :(得分:0)
您似乎错过了每个选择器中的结束括号。我说这是因为它无法将值插入输入字段。
此外,请确保在将这些值插入输入字段之前收集并存储这些值。
答案 1 :(得分:0)
尝试在函数getParameterByName()上面声明所有变量,并在函数内部加载变量及其值,在完成加载变量后立即添加jQuery函数作为简单$(函数) (){})。
我想问题是当你的$(document).ready()函数在页面加载时执行,并且没有值加载YET。
<script>
var visitor_id;
var medium;
var source;
var campaign;
var content;
var term;
var count_of_sessions;
var count_of_page_views;
// Parse the URL
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
// Give the URL parameters variable names
visitor_id = getParameterByName('utm_visitor_id');
medium = getParameterByName('utm_medium');
source = getParameterByName('utm_source');
campaign = getParameterByName('utm_campaign');
content = getParameterByName('utm_content');
term = getParameterByName('utm_term');
count_of_sessions = getParameterByName('utm_count_of_sessions');
count_of_page_views = getParameterByName('utm_count_of_page_views');
// Set the cookies
Cookies.set('utm_visitor_id', visitor_id, { expires: 90 });
Cookies.set('utm_medium', medium, { expires: 90 });
Cookies.set('utm_source', source, { expires: 90 });
Cookies.set('utm_campaign', campaign, { expires: 90 });
Cookies.set('utm_content', content, { expires: 90 });
Cookies.set('utm_term', term, { expires: 90 });
Cookies.set('utm_count_of_sessions', count_of_sessions, { expires: 90 });
Cookies.set('utm_count_of_page_views', count_of_page_views, { expires: 90 });
// Grab the cookie value and set the form field values
$(function(){
$('input[name=00N4100000NglQ2').val(utm_visitor_id);
$('input[name=00N4100000NgleO').val(utm_medium);
$('input[name=00N4100000NglpC').val(utm_source);
$('input[name=00N4100000NglyY').val(utm_campaign);
$('input[name=00N4100000NgmBh').val(utm_content);
$('input[name=00N4100000NgmIs').val(utm_term);
$('input[name=00N4100000NgmQw').val(utm_count_of_sessions);
$('input[name=00N4100000NgmZ5').val(utm_count_of_page_views);
});
}
</script>
&#13;