将cookie设置为具有表单条目的值

时间:2023-01-09 19:39:51

Asked a related question earlier, but it looks like it was lacking a context, so, couldn't make it work. Trying to set a cookie value of a form entry on click (using carhartl jquery plugin). And getting nothing. Not even errors. Cookies plugin is included into head portion of the page.

之前被问到一个相关的问题,但看起来它缺乏一个上下文,所以无法使它工作。尝试在单击时设置表单条目的cookie值(使用carhartl jquery插件)。一无所获。甚至没有错误。 Cookies插件包含在页面的头部。

html of it

它的HTML

    <input type="text" class="signfield emfield" />
    <input type="text" class="signfield nam1field" />
    <input type="text" class="signfield nam2field" />
    <div class="submt sbmtfrm" style="cursor:pointer;">Step 2</div>

and jquery

和jquery

$(document).ready(function(window, $){
    var emailvar;
    var name1var;
    var name2var;
    $(".sbmtfrm").click(function(){
        emailvar = $(".emfield").val();
        name1var = $(".nam1field").val();
        name2var = $(".nam2field").val();
        alert(emailvar);
        $.cookie("sec8email", emailvar);
        $.cookie("sec8name1", name1var);
        $.cookie("sec8name2", name2var);
    });

    var emvar = $.cookie("sec8email");
    var name1 = $.cookie("sec8name1");
    var name2 = $.cookie("sec8name2");

}(window, jQuery));

1 个解决方案

#1


0  

The function you pass into ready is actually executed before the DOM is ready since you call it. This should call it when the DOM is ready and still maintain what you want to do.

你准备好的函数实际上是在你准备好DOM之前执行的。这应该在DOM准备就绪时调用它并仍然保持您想要做的事情。

(function(window, $){
    $(document).ready(function(){
        var emailvar;
        var name1var;
        var name2var;
        $(".sbmtfrm").click(function(){
            emailvar = $(".emfield").val();
            name1var = $(".nam1field").val();
            name2var = $(".nam2field").val();
            alert(emailvar);
            $.cookie("sec8email", emailvar);
            $.cookie("sec8name1", name1var);
            $.cookie("sec8name2", name2var);
        });

        var emvar = $.cookie("sec8email");
        var name1 = $.cookie("sec8name1");
        var name2 = $.cookie("sec8name2");

    });
}(window, jQuery));

function(window,jQuery){...}(window,jQuery) will execute the function and return undefined, so you would be doing $document.ready(undefined)

function(window,jQuery){...}(window,jQuery)将执行该函数并返回undefined,因此你将执行$ document.ready(undefined)

#1


0  

The function you pass into ready is actually executed before the DOM is ready since you call it. This should call it when the DOM is ready and still maintain what you want to do.

你准备好的函数实际上是在你准备好DOM之前执行的。这应该在DOM准备就绪时调用它并仍然保持您想要做的事情。

(function(window, $){
    $(document).ready(function(){
        var emailvar;
        var name1var;
        var name2var;
        $(".sbmtfrm").click(function(){
            emailvar = $(".emfield").val();
            name1var = $(".nam1field").val();
            name2var = $(".nam2field").val();
            alert(emailvar);
            $.cookie("sec8email", emailvar);
            $.cookie("sec8name1", name1var);
            $.cookie("sec8name2", name2var);
        });

        var emvar = $.cookie("sec8email");
        var name1 = $.cookie("sec8name1");
        var name2 = $.cookie("sec8name2");

    });
}(window, jQuery));

function(window,jQuery){...}(window,jQuery) will execute the function and return undefined, so you would be doing $document.ready(undefined)

function(window,jQuery){...}(window,jQuery)将执行该函数并返回undefined,因此你将执行$ document.ready(undefined)