开发者

JQuery code problem?

New to JQuery, I added the following JQuery code below and moved it around in my code and now it won't work I forgot what I did, can so开发者_开发知识库meone fix my code by placing the below code in its correct place thanks.

$('a').click(function () {
    $('#changes-saved').remove();
});
    return false; // prevent normal submit
});

JQuery code.

$(function() {
    $('#changes-saved').hide();
    $('.save-button').click(function() {
        $.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(html) {
            $('div.contact-info-form').html(html);
            $('#changes-saved').append('Changes saved!').show().pause(1000).hide();
        });
        return false; // prevent normal submit
    });

    $('a').click(function () {
        $('#changes-saved').remove();
    });
        return false; // prevent normal submit
    });
});


It looks like an extra paste resulting in an extra return and closing block, just remove this at the end:

    return false; // prevent normal submit
});


To prevent <a> from propagating in the browser.

$('a').click(function (event) {
    if (!event) event = window.event;
    if (event.preventDefault)
       event.preventDefault();
    else
       event.returnValue = false;


    $('#changes-saved').remove();

    //In case the event propagation didn't work....(mostly dumb IE)
    return false; // prevent normal submit
});

From your code, just fix

$(function() {
    $('#changes-saved').hide();
    $('.save-button').click(function() {
        $.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(html) {
            $('div.contact-info-form').html(html);
            $('#changes-saved').append('Changes saved!').show().pause(1000).hide();
        });
        return false; // prevent normal submit
    });
    $('a').click(function () {
        $('#changes-saved').remove();
        return false; // prevent normal submit
    });
    });
});


Is this what you wanted? Had an extra )}; at the end and return false; was not inside the click event handler function.

$(function() {
    $('#changes-saved').hide();

    $('.save-button').click(function() {
        $.post(
            $('#contact-form').attr('action'), 
            $('#contact-form').serialize(), 
            function(html) {
                $('div.contact-info-form').html(html);
                $('#changes-saved').append('Changes saved!')
                    .show().pause(1000).hide();
            }
        );

        return false; // prevent normal submit
    });

    $('a').click(function () {
        $('#changes-saved').remove();

        return false; // prevent normal submit
    });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜