简体   繁体   English

缺少:在JQuery.inArray(值,数组)中的属性ID之后

[英]missing : after property id in JQuery.inArray(value, array)

I'm getting a firebug error: 我收到萤火虫错误:

missing : after property id error source line: 缺少:属性ID错误源行之后:

if(jQuery.inArray(mmDialogButton.CANCEL, buttons)){ 

This is the surrunding code: 这是多余的代码:

Edited post with update as I was unclear. 我不清楚如何编辑更新的帖子。 I am trying to create a framework for creating dialogues for a project. 我正在尝试创建一个用于为项目创建对话的框架。 In the dialogs there can be four predefined buttons. 在对话框中,可以有四个预定义的按钮。 The mmDialogButton is my attempt to an ENUM class. mmDialogBu​​tton是我尝试的ENUM类。 The if statement is there to enable the buttons the user wanted to use in the dialog. 此处的if语句用于启用用户想要在对话框中使用的按钮。

Here is some more code to illustrate. 这是一些更多的代码来说明。

mmDialog.js mmDialog.js

...

function mmDialog(title, spawnerId, widget, buttons){
...
$dialog.html(widget.getInitialHTML())
        .dialog({
            autoOpen: false,
            title: title + ' <img id="myJquerySpinner" />',
            buttons: {
                if(jQuery.inArray(mmDialogButton.CANCEL, buttons)){
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    },
                }
                if(jQuery.inArray(mmDialogButton.NEXT, buttons)){
                    "Next": function() {
                        widget.doNext();
                    },
                }
                if(jQuery.inArray(mmDialogButton.PREVIOUS, buttons)){
                    "Previous": function() {
                        widget.doPrevious();
                    },
                }
                if(jQuery.inArray(mmDialogButton.OK, buttons)){
                    "Ok": function() {
                        widget.doOk();
                    }
                }
            }...

mmDialogButton.js mmDialogBu​​tton.js

function mmDialogButton(){  // Constructor

}

mmDialogButton.CANCEL = function() { return "mmDBCancel"; };
mmDialogButton.OK = function() { return "mmDBOk"; };
mmDialogButton.NEXT = function() { return "mmDBNext"; };
mmDialogButton.PREVIOUS = function() { return "mmDBPrevious"; };

jsp/html page jsp / html页面

            var title = "Test Dialog";
            var spawnerId = "myJqueryStarter";

            var mmDialogButtons = new Array();
            mmDialogButtons[0] = mmDialogButton.CANCEL; 
            mmDialogButtons[1] = mmDialogButton.OK; 
            mmDialogButtons[2] = mmDialogButton.NEXT; 
            mmDialogButtons[3] = mmDialogButton.PREVIOUS; 

            myPublishWidget = new mmPublishWidget();
            myDialogPublishWidget = new mmDialogWidget(myPublishWidget);
            myDialog = new mmDialog(title, spawnerId, myDialogPublishWidget , mmDialogButtons);

This: 这个:

buttons: {
    if(jQuery.inArray(mmDialogButton.CANCEL, buttons)){
      Cancel: function() {
      $( this ).dialog( "close" );
    },

should probably be: 应该可能是:

buttons: (function() {
    if(jQuery.inArray(mmDialogButton.CANCEL, buttons))
      return {
        Cancel: function() {
          $( this ).dialog( "close" );
         }
       };
       return null;
    })()

though it's hard to tell. 虽然很难说。 What it looks like you're trying to do is conditionally set that "buttons" property to some object with a labeled handler (that little "close" function). 看起来像你试图做的是有条件设置“按钮”属性的一些对象与标记的处理程序(即小“关闭”功能)。 However, the code you posted is syntactically nonsensical. 但是,您发布的代码在语法上是荒谬的。 The change I made wraps the "inArray" test in an anonymous function that returns the button object only when that test is true . 我所做的更改将“ inArray”测试包装在一个匿名函数中,该函数仅在该测试为true时才返回按钮对象。

Again, I'm just guessing that that's what you were trying to do. 再次,我只是猜测那就是您要尝试做的。

I think you mean to execute the "close" only if CANCEL is in buttons, if it's the case you can write: 我认为您的意思是仅在按钮中取消时才执行“关闭”,如果是这样的话,您可以编写:

buttons: {
    Cancel: function() {
              if(jQuery.inArray(mmDialogButton.CANCEL, buttons)){
                 $( this ).dialog( "close" );
              }
            },
    ....

EDIT: 编辑:

you can define the buttons dictionary beforehand as you like, the pass it to .dialog( : 您可以根据需要预先定义按钮字典,并将其传递给.dialog(

dialog_buttons = {}

if(jQuery.inArray(mmDialogButton.CANCEL, buttons)){
 dialog_buttons[Cancel] = function() {
                             $( this ).dialog( "close" );
                          }
}

if(jQuery.inArray(mmDialogButton.NEXT, buttons)){
 dialog_buttons["Next"] = function() {
                             widget.doNext();
                          }
}

if(jQuery.inArray(mmDialogButton.PREVIOUS, buttons)){
 dialog_buttons["Previous"] = function() {
                                 widget.doPrevious();
                              }
}

if(jQuery.inArray(mmDialogButton.OK, buttons)){
 dialog_buttons["Ok"] = function() {
                           widget.doOk();
                        }
}

$dialog.html(widget.getInitialHTML())
        .dialog({
            autoOpen: false,
            title: title + ' <img id="myJquerySpinner" />',
            buttons: dialog_buttons
            }...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM