尝试使用 js-cookies 设置表单输入值

Trying to set form input value using js-cookies

我有一个登陆页面,当你到达时,它会在你的机器上设置一个 cookie:

<script type="text/javascript">
    Cookies.set('Web_Lead_Specific_Source__c', 'PPC', { expires: 365 });
</script>

在一个单独的页面上,我有一个带有隐藏字段的表单:

<input name="Web_Lead_Specific_Source__c" value="">

如果用户访问了着陆页,我将尝试使用 "PPC" 加载该值。以下代码无效:

Cookies.get('Web_Lead_Specific_Source__c');  => 'PPC'

我确定我遗漏了什么。

仅仅获取cookie是不够的,还需要在输入框里设置:

var leadType = Cookies.get('Web_Lead_Specific_Source__c');
var target = document.getElementsByName('Web_Lead_Specific_Source__c')[ 0 ];
target.value = leadType;

js-cookie 只负责设置和获取 浏览器 cookies 的值,它对页面中的 元素一无所知,那是不一样的。

下面是一个工作示例,使用正确的面向对象命名以防止混淆:http://jsfiddle.net/f1fkydwh/