Javascript 用 jQuery 枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3625805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:28:32  来源:igfitidea点击:

Enums with jQuery?

javascriptjqueryenums

提问by chugh97

$("#bc [id$=_dropdownID]").change(function() {
    if (this.value == '2' || this.value == '3') {
        $("#bc .pnl").show();
    }
    else {
        $("#bc .pnl").hide();
    }

I have the following code in jQuery. Is there any way I can replace the hard coded constants 2 and 3 in the above code with a c# enum? Does jQuery support enums and if so how can this be achieved? Any suggestions welcome....

我在 jQuery 中有以下代码。有什么办法可以用 ac# enum 替换上面代码中的硬编码常量 2 和 3?jQuery 是否支持枚举,如果支持,如何实现?欢迎任何建议......

回答by sirrocco

You would have to duplicate the enum in JavaScript like so:

您必须像这样在 JavaScript 中复制枚举:

var myEnum = {
         OneValue: 2,
         AnotherValue: 3
};

then you can use it like this:

那么你可以像这样使用它:

this.value === myEnum.OneValue || this.value === myEnum.AnotherValue;