javascript - Closing braces and parentheses - Stack Overflow

ProblemI've bined three separate pieces of code i've been using and am having trouble closing

Problem

I've bined three separate pieces of code i've been using and am having trouble closing the braces and parentheses. Looking at the code i suspect it may not be correct also, although i'm not entirely sure as still in the process of learning JavaScript.

 $('#form1').submit(function(e) {
        var currentForm = this;
        e.preventDefault();
        bootbox.confirm("Are you sure?", function(result) {

            if (result) {

                console.log('Form Submitted'); //Debug line
        $.ajax({
            type: 'POST',
            url: 'indextest4.php',
            data: $("#form1").serialize(),
            error: function () {
                console.log('Ajax Error');}, //Debug Line

            success: function (response) {

                console.log('Ajax Success'); //Debug Line

                $('.fConfirm').css({
                display:"inline", 
                height: 680, 
                width: 940
                });

                console.log("After CSS change");

            }
        });

    });

Also if some kind soul could perhaps tell me a rule of thumb of how i could work out when you close using?

}

})

});

Problem

I've bined three separate pieces of code i've been using and am having trouble closing the braces and parentheses. Looking at the code i suspect it may not be correct also, although i'm not entirely sure as still in the process of learning JavaScript.

 $('#form1').submit(function(e) {
        var currentForm = this;
        e.preventDefault();
        bootbox.confirm("Are you sure?", function(result) {

            if (result) {

                console.log('Form Submitted'); //Debug line
        $.ajax({
            type: 'POST',
            url: 'indextest4.php',
            data: $("#form1").serialize(),
            error: function () {
                console.log('Ajax Error');}, //Debug Line

            success: function (response) {

                console.log('Ajax Success'); //Debug Line

                $('.fConfirm').css({
                display:"inline", 
                height: 680, 
                width: 940
                });

                console.log("After CSS change");

            }
        });

    });

Also if some kind soul could perhaps tell me a rule of thumb of how i could work out when you close using?

}

})

});

Share Improve this question asked Mar 11, 2014 at 23:09 Dan CundyDan Cundy 2,8692 gold badges40 silver badges65 bronze badges 18
  • 2 Any decent coding-oriented text editor/IDE will do parentheses/bracket matching for you. – Pointy Commented Mar 11, 2014 at 23:10
  • I'm using Dreamweaver, which just throws errors at you and not possible solutions. – Dan Cundy Commented Mar 11, 2014 at 23:12
  • 1 } closes a function definition or a conditional block. ) closes a set of function arguments or an expression. }) are used when it's both the end of a function definition and the end of function arguments (which is the case when passing an anonymous function as the last argument to a function). The }) isn't anything special, just the bination of the two previous rules. – jfriend00 Commented Mar 11, 2014 at 23:28
  • 2 Also, if your editor isn't helping you, you can always paste a block of code into a jsBeautifier and it will often help you a lot too because it makes the indentation match the bracing often showing you were there are faults. I use this one. – jfriend00 Commented Mar 11, 2014 at 23:36
  • 1 Ok, found it: stackoverflow./a/10372059/2672018. No excuse anymore (and if you make money with a product that costs, don't use it for free). – loveNoHate Commented Mar 11, 2014 at 23:58
 |  Show 13 more ments

5 Answers 5

Reset to default 2

Correct indentation helps a lot here since everything will line up nicely after. Here is what I think you are looking for:

$('#form1').submit(function(e) {
    var currentForm = this;
    e.preventDefault();
    bootbox.confirm("Are you sure?", function(result) {
        if (result) {
            console.log('Form Submitted'); //Debug line
            $.ajax({
                type: 'POST',
                url: 'indextest4.php',
                data: $("#form1").serialize(),
                error: function () {
                    console.log('Ajax Error');
                }, //Debug Line

                success: function (response) {

                    console.log('Ajax Success'); //Debug Line

                    $('.fConfirm').css({
                        display:"inline", 
                        height: 680, 
                        width: 940
                    });

                    console.log("After CSS change");

                } // success: ...
            }); // $.ajax ...
        } // if (result) ...
    }); // bootbox.confirm ...
}); // $('#form1').submit ...

I added ments on all closing braces at the end to show what they correspond to.

As other pointed out, the key here is to indent your code, and use a good editor:

Not a rule of thumb but one tip is to add a tab space as you nesting block of code.

Most editor will let you ident the code by selecting it and using keyboard shortcuts:

to the left:

Shift + Tab

to the right:

Tab

Your last line should be two curly braces. As with the ments, get a good editor like Notepad++ they will show you the open and close braces, tags, etc

This may not fix all of your problems, but it does fix the opening and closing braces.

    $('#form1').submit(function(e) {  //start form submit handler
        var currentForm = this;
        e.preventDefault();
        bootbox.confirm("Are you sure?", function(result) {  //start bootbox.confirm callback
            if (result) {                      //start if block
                console.log('Form Submitted');
                $.ajax({                        //start ajax call
                    type: 'POST',
                    url: 'indextest4.php',
                    data: $("#form1").serialize(),
                    error: function () {            //start ajax error callback
                        console.log('Ajax Error');
                    },                              //end ajax error callback

                    success: function (response) {  //start ajax success callback
                        console.log('Ajax Success'); 
                        $('.fConfirm').css({         //start css change
                            display:"inline", 
                            height: 680, 
                            width: 940
                        });                          // end css change
                        console.log("After CSS change");
                    }                                //end ajax success callback
               });   //end ajax call
           }    //end if block
       });  //end bootbox.confirm
   }); //end form submit handler

It just takes a little practice, but the basic rule of thumb is to close everything you open. Whether it's a '(' or a '{' .

This should be correct:

$('#form1').submit(function(e) {
    var currentForm = this;
    e.preventDefault();
    bootbox.confirm("Are you sure?", function(result) {

        if (result) {

            console.log('Form Submitted'); //Debug line
            $.ajax({
                type: 'POST',
                url: 'indextest4.php',
                data: $("#form1").serialize(),
                error: function () {
                    console.log('Ajax Error');}, //Debug Line

                success: function (response) {

                    console.log('Ajax Success'); //Debug Line

                    $('.fConfirm').css({
                        display:"inline", 
                        height: 680, 
                        width: 940
                    });

                    console.log("After CSS change");

                }
            });
        }
    });
});

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

相关推荐

  • javascript - Closing braces and parentheses - Stack Overflow

    ProblemI've bined three separate pieces of code i've been using and am having trouble closing

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信