开发者

Jquery cookie plugin - set cookie when user submits form

I would like to set a cookie when a user submits a form.

I am working with some legacy code that sets a cookie based on a user checking a checkbox, but would like this to simply set the cookie when a user submits a form.

Here is what i have, which works when a checkbox is checked:

$().ready(function()
{
    $('.application-form').submit(function(e)
    {
        e.preventDefault();

        if ($('.application-form input[name=marketing]').is(':checked'))
        {
            $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
        }
    });
});

This works fine, but i don't want the cookie set when checking the "marketing" input, rather just when the form submits. Like this:

$().ready(function()
{
    $('.application-form').submit(function(e)
    {
        e.preventDef开发者_开发技巧ault();

        if ($('.application-form').submit();)
        {
            $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
        }
    });
});

However, this does not set the cookie successfully, any ideas would be great,thanks.


You don't need to check for submit return value in if condition.

$().ready(function()
{
    $('.application-form').submit(function(e)
    {
        e.preventDefault();

        $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
    });
});


$(document).ready(function() {
  $('.application-form').submit(function(e) {
    e.preventDefault();
    $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
  });
});

You don't need the if condition there, because it seems to be testing whether the form has been submitted or not, and you already know that it has, because you're in the form's submit event handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜