.Change()时jQuery Cookie保存FORM值

时间:2013-11-21 01:56:12

标签: jquery jquery-plugins

我通过jQuery Cookie将{​​{1}}值传递给cookie。我做错了什么我无法获得要保存的cookie值

FORM

HTML

<script src="/assets/jquery.cookie.js"></script>
<script type="text/javascript">
;(function ($) {
    $('input[name="FirstName"]').change(function(){
        $.cookie("FirstName", $('input[name="FirstName"]').val();, {path: "/", domain: "domain.com"});
    });
    $('input[name="LastName"]').change(function(){
        $.cookie("LastName", $('input[name="LastName"]').val();, {path: "/", domain: "domain.com"});
    });
    $('input[name="Email"]').change(function(){
        $.cookie("Email", $('input[name="Email"]').val();, {path: "/", domain: "domain.com"});
    });
    $('input[name="Category"]').change(function(){
        $.cookie("Category", $('select[name="Category"]').val();, {path: "/", domain: "domain.com"});
    });
})(jQuery);
</script>

1 个答案:

答案 0 :(得分:1)

您的代码中存在语法错误,请移除;

$.cookie("FirstName", $('input[name="FirstName"]').val();, {path: "/", domain: "domain.com"});
                                                // ---- ^

另请注意,;(function ($) { ... })(jQuery)是一个闭包,它不会执行文件就绪处理程序所做的事情。

jQuery(function($) { // The first argument of the document ready handler refers to jQuery
    $('input, select').change(function(){
        $.cookie(this.name, this.value, {path: "/", domain: "domain.com"});
    });
});