javascript - Submit form after preventDefault and unbind - Stack Overflow

I used preventDefault on my form and I am trying to use this to automatically submit when a statement i

I used preventDefault on my form and I am trying to use this to automatically submit when a statement is true.

function codeCheck(){
        $("#code-form").submit(function(e){
            e.preventDefault();
        });
        var code = $("#code").val();
        $.ajax({
            url: "index.php",
            type: "POST",
            data: { codeCheck: 1, ajaxCode: code }
        }).done(function(check) {
            if(check == 2){
                $("#used").css("display", "block");
            }
            else if(check == 1){
                $("#code").css('background', "url('../img/check.png') no-repeat scroll 170px 2px");
                $("#submit").css('display', 'block');
                $("#submit").on("click", function(){
                    $( "#container" ).fadeOut( "slow", function() {
                        $("#code-form")[0].submit();
                    });
                });

                $("#used").css("display", "none");
            }
            else{
                $("#code").css('background', "none");
                $("#used").css("display", "none");
                //$("#code-form").unbind('submit');
            }
        }); 
}

The unbind does work I can use the submit button when I unbind it. But the $("#form").submit() part doesn't work it doesn't submit the form automatically after fading out.

Here is a fiddle: /

All I want is that it continues the form after the fade.

EDIT: For more clarity I just added the full function.

I used preventDefault on my form and I am trying to use this to automatically submit when a statement is true.

function codeCheck(){
        $("#code-form").submit(function(e){
            e.preventDefault();
        });
        var code = $("#code").val();
        $.ajax({
            url: "index.php",
            type: "POST",
            data: { codeCheck: 1, ajaxCode: code }
        }).done(function(check) {
            if(check == 2){
                $("#used").css("display", "block");
            }
            else if(check == 1){
                $("#code").css('background', "url('../img/check.png') no-repeat scroll 170px 2px");
                $("#submit").css('display', 'block');
                $("#submit").on("click", function(){
                    $( "#container" ).fadeOut( "slow", function() {
                        $("#code-form")[0].submit();
                    });
                });

                $("#used").css("display", "none");
            }
            else{
                $("#code").css('background', "none");
                $("#used").css("display", "none");
                //$("#code-form").unbind('submit');
            }
        }); 
}

The unbind does work I can use the submit button when I unbind it. But the $("#form").submit() part doesn't work it doesn't submit the form automatically after fading out.

Here is a fiddle: http://jsfiddle/90wmpw7x/1/

All I want is that it continues the form after the fade.

EDIT: For more clarity I just added the full function.

Share Improve this question edited Nov 12, 2014 at 15:52 Sinan Samet asked Nov 12, 2014 at 14:33 Sinan SametSinan Samet 6,76213 gold badges54 silver badges97 bronze badges 7
  • 1 The .unbind() method is for removing an event handler; it doesn't invoke the event handler. – Pointy Commented Nov 12, 2014 at 14:34
  • Oh you are right I misunderstood that part. – Sinan Samet Commented Nov 12, 2014 at 14:37
  • If you put alert('Here'); inside the unbind callback do you see it? – PeterKA Commented Nov 12, 2014 at 14:42
  • No I just tested it and the unbind doesn't work like that indeed. But I saw it somewhere on stackoverflow and it seemed to work but I tested it wrong. – Sinan Samet Commented Nov 12, 2014 at 14:44
  • Quite likely, you have not bound any handler to the submit event; therefore there's nothing to unbind. – PeterKA Commented Nov 12, 2014 at 14:47
 |  Show 2 more ments

2 Answers 2

Reset to default 4

You want to use the following, since, you say submit event is already prevented. This will submit the form in the default manner:

$("#form")[0].submit();

Instead of:

$("#form").unbind('submit', function(){
    $("#form").submit();    
});

Special Note:

For this to work, ensure you do not have any form field with name="submit".

UPDATE

Given the updated code, here is my remendation:

$("#form").submit(function(e){
    e.preventDefault();

    $( "#container" ).fadeOut( "slow", function() {
        $("#form")[0].submit();    
    });
});

The fadeOut code should be moved to inside the submit handler

DEMO

You don't need to unbind() as the page is refreshed and all event handlers will be destroyed.

$("#container").fadeOut("slow", function() {
    $("#form").submit();
});

But, if you've event handler attached and you do posting through ajax and want to unbind that event, then you can use chaining

$('#form').unbind('submit').submit();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742360776a4429348.html

相关推荐

  • javascript - Submit form after preventDefault and unbind - Stack Overflow

    I used preventDefault on my form and I am trying to use this to automatically submit when a statement i

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信