如何根据 Javascript 中的变量设置属性值

发布于 2024-11-30 00:21:50 字数 739 浏览 0 评论 0原文

问题是我有这段代码(Jquery UI):

    $("#dialog-confirm").dialog({
        resizable: false,
        modal: true,
        buttons: {
            "Remove": function() {
                $(this).dialog("close");
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });

现在我必须通过为每个按钮提供单词翻译来将其国际化。我对变量 STR_REMOVE 和 STR_CANCEL 进行了翻译,但如果我执行类似

        buttons: {
            STR_REMOVE: function() {
                $(this).dialog("close");
            },
            STR_CANCEL: function() {
                $(this).dialog("close");
            }
        }

按钮(属性)的操作,则采用值“STR_REMOVE”和“STR_CANCEL”,而不是其内容。那么,问题是,我能做什么?

The problem is I have this piece of code (Jquery UI):

    $("#dialog-confirm").dialog({
        resizable: false,
        modal: true,
        buttons: {
            "Remove": function() {
                $(this).dialog("close");
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });

and now I have to internationalize it by giving to each button a word translation. I have the translation on the variables STR_REMOVE and STR_CANCEL, but if I do something like

        buttons: {
            STR_REMOVE: function() {
                $(this).dialog("close");
            },
            STR_CANCEL: function() {
                $(this).dialog("close");
            }
        }

the buttons (the properties) take the value "STR_REMOVE" and "STR_CANCEL", not its content. So, the question is, what can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开半夏魅人心 2024-12-07 00:21:50

试试这个。

var STR_REMOVE = "my delete",  STR_CANCEL = "my cancel";

$("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    buttons: [{
        text: STR_REMOVE,
        click: function () {
            $(this).dialog("close");
        }
    }, 
    {
        text: STR_CANCEL,
        click: function () {
            $(this).dialog("close");
        }
    }]
});

看看 jquery ui 文档: http://jqueryui.com/demos/dialog/#选项按钮

Try this.

var STR_REMOVE = "my delete",  STR_CANCEL = "my cancel";

$("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    buttons: [{
        text: STR_REMOVE,
        click: function () {
            $(this).dialog("close");
        }
    }, 
    {
        text: STR_CANCEL,
        click: function () {
            $(this).dialog("close");
        }
    }]
});

have a look at the jquery ui doc: http://jqueryui.com/demos/dialog/#option-buttons

ら栖息 2024-12-07 00:21:50
var buttons = {};
buttons[STR_REMOVE] = function() {
                $(this).dialog("close");
            };
buttons[STR_CANCEL] = function() {
                $(this).dialog("close");
            };
$("#dialog-confirm").dialog({
     resizable: false,
     modal: true,
     buttons: buttons
});
var buttons = {};
buttons[STR_REMOVE] = function() {
                $(this).dialog("close");
            };
buttons[STR_CANCEL] = function() {
                $(this).dialog("close");
            };
$("#dialog-confirm").dialog({
     resizable: false,
     modal: true,
     buttons: buttons
});
一萌ing 2024-12-07 00:21:50

您不能内联执行此操作。您必须首先声明对象,然后使用方括号成员运算符 设置属性:

var buttons = {};
buttons[STR_REMOVE] = function() {
    $(this).dialog("close");
};
buttons[STR_CANCEL] = function() {
    $(this).dialog("close");
};

$("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    buttons: buttons
});

You can't do this inline. You'll have to declare the object first, then use the square bracket member operator to set the properties:

var buttons = {};
buttons[STR_REMOVE] = function() {
    $(this).dialog("close");
};
buttons[STR_CANCEL] = function() {
    $(this).dialog("close");
};

$("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    buttons: buttons
});
赠我空喜 2024-12-07 00:21:50

试试这个,未测试:

$("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    buttons: {
        "Remove": function() {
            $(this).html(STR_REMOVE);
            $(this).dialog("close");
        },
        "Cancel": function() {
            $(this).html(STR_REMOVE);
            $(this).dialog("close");
        }
    }
});

Try this, not tested:

$("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    buttons: {
        "Remove": function() {
            $(this).html(STR_REMOVE);
            $(this).dialog("close");
        },
        "Cancel": function() {
            $(this).html(STR_REMOVE);
            $(this).dialog("close");
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文