小编Nav*_*ddy的帖子

如何避免JQuery中的if/else循环

下面的代码工作得很好,但if/else语句看起来很长很难看.有没有办法可以避免这些陈述?

CommissionTypeFilterVm是一个列表,其中包含下面显示的6个属性,其值为10.我想根据这些属性的值启用和禁用复选框:

CommissionType
CommissionTrials
OverrideType
OverrideTrials
BonusType
AdjustmentType
Run Code Online (Sandbox Code Playgroud)

这是代码:

   if (view != null) 
        {
            if (view.CommissionTypeFilterVm !=null && view.CommissionTypeFilterVm.length>0)
            {
                if (view.CommissionTypeFilterVm[0].CommissionType != 1) {
                    this.$commissionType.prop("checked", false).prop("disabled", true);
                } else {
                    this.$commissionType.prop("disabled", false).prop("checked", true);
                }
                if (view.CommissionTypeFilterVm[0].CommissionTrials != 1) {
                    this.$commissionTrails.prop("checked", false).prop("disabled", true);
                } else {
                    this.$commissionTrails.prop("disabled", false).prop("checked", true);
                }
                if (view.CommissionTypeFilterVm[0].OverrideType != 1) {
                    this.$overrideType.prop("checked", false).prop("disabled", true);
                } else {
                    this.$overrideType.prop("disabled", false).prop("checked", true);
                }
                if (view.CommissionTypeFilterVm[0].OverrideTrials != 1) {
                    this.$overrideTrails.prop("checked", false).prop("disabled", true);
                } else …
Run Code Online (Sandbox Code Playgroud)

javascript jquery refactoring

2
推荐指数
1
解决办法
726
查看次数

标签 统计

javascript ×1

jquery ×1

refactoring ×1