【问题标题】:Closing braces and parentheses右大括号和圆括号
【发布时间】:2014-03-11 23:09:58
【问题描述】:

问题

我已经组合了我一直在使用的三段单独的代码,但在关闭大括号和圆括号时遇到了麻烦。查看代码我怀疑它也可能不正确,尽管我不完全确定是否仍在学习 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");

            }
        });

    });

另外,如果某个好心人可以告诉我一个经验法则,当你关闭使用时我可以如何解决?

}

})

});

【问题讨论】:

  • 任何像样的面向编码的文本编辑器/IDE都会为你做括号/括号匹配。
  • 我正在使用 Dreamweaver,它只会向您抛出错误,并且无法解决问题。
  • } 关闭函数定义或条件块。 ) 关闭一组函数参数或表达式。 }) 用于函数定义的结尾和函数参数的结尾(将匿名函数作为函数的最后一个参数传递时就是这种情况)。 }) 没有什么特别之处,只是前面两条规则的组合。
  • 另外,如果您的编辑器没有帮助您,您可以随时将代码块粘贴到 jsBeautifier 中,它通常也会对您有很大帮助,因为它使缩进与经常显示的支撑相匹配是否有故障。我使用this one
  • 好的,找到了:stackoverflow.com/a/10372059/2672018。没有任何借口了(如果你用昂贵的产品赚钱,不要免费使用它)。

标签: javascript


【解决方案1】:

正确的缩进在这里很有帮助,因为之后一切都会很好地排列。以下是我认为您正在寻找的内容:

$('#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 ...

我在最后的所有右大括号上添加了 cmets 以显示它们对应的内容。

【讨论】:

    【解决方案2】:

    正如其他人指出的,这里的关键是缩进您的代码,并使用一个好的编辑器

    不是经验法则,但一个提示是在嵌套代码块时添加一个制表符空间。

    大多数编辑器都会让您通过选择代码并使用键盘快捷键来识别代码:

    向左:

    Shift + Tab

    向右:

    制表符

    【讨论】:

    【解决方案3】:

    最后一行应该是两个花括号。和 cmets 一样,找一个像 Notepad++ 这样的好编辑器,它们会向您显示开闭大括号、标签等

    【讨论】:

      【解决方案4】:

      这可能无法解决您的所有问题,但它确实解决了左大括号和右大括号。

          $('#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
      

      只需要一点练习,但基本的经验法则是关闭您打开的所有内容。无论是 '(' 还是 '{' 。

      【讨论】:

      • 你的缩进有什么问题?是8个空格还是4个空格?
      • 看起来我在第一个缩进中使用了 8。感谢您指出。我修好了
      • 第一行有 4 个空格,最后一行还有 3 个空格。
      【解决方案5】:

      这应该是正确的:

      $('#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");
      
                      }
                  });
              }
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2021-01-11
        • 2021-09-15
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 2011-09-26
        • 2019-02-03
        相关资源
        最近更新 更多