Javascript 我的while循环不是';不行吗?

Javascript 我的while循环不是';不行吗?,javascript,jquery,Javascript,Jquery,我已经写了这段简单的代码,它很有效 $(function () { $('form').each(function () { var form = $(this); form.find('[class^="custAction_"]').prop('disabled', true).trigger("chosen:updated"); form.find('[class^="custAction_4b"]').button('dis

我已经写了这段简单的代码,它很有效

    $(function () {
    $('form').each(function () {
        var form = $(this);
        form.find('[class^="custAction_"]').prop('disabled', true).trigger("chosen:updated");
        form.find('[class^="custAction_4b"]').button('disable');
        form.find('.custSwitch_1').change(function () {
            if (form.find('.custSwitch_1:checked').length) {
                form.find('.custAction_1').prop('disabled', false).trigger("chosen:updated").trigger("change");
          } else {
                form.find('.custAction_1').prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
            }
        });
        form.find('.custSwitch_2').change(function () {
            if (form.find('.custSwitch_2:checked').length) {
                form.find('.custAction_2').prop('disabled', false).trigger("chosen:updated").trigger("change");
            } else {
                form.find('.custAction_2').prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
            }
        });
        form.find('.custSwitch_3').change(function () {
            if (form.find('.custSwitch_3:checked').length) {
                form.find('.custAction_3').prop('disabled', false).trigger("chosen:updated").trigger("change");
            } else {
                form.find('.custAction_3').prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
            }
        });
        form.find('.custSwitch_4').change(function () {
            if (form.find('.custSwitch_4:checked').length) {
                form.find('.custAction_4').prop('disabled', false).trigger("chosen:updated").trigger("change");
                form.find('.custAction_4b').button("enable");
          } else {
                form.find('.custAction_4').prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
                form.find('.custAction_4b').prop("checked", false).button("refresh").button("disable", "disable");

            }
        });
    });
});
知道这是重复的,我认为“while”循环可以在这里工作,所以尝试了这个

        $(function () {
        $('form').each(function () {
            var form = $(this);
            var switchClass = $('form[class^="custSwitch_"]').length;
            form.find('[class^="custAction_"]').prop('disabled', true).trigger("chosen:updated");
            form.find('[class^="custAction_4b"]').button('disable');
            var countSw = 1;
            while (countSw < switchClass) {
                form.find('.custSwitch_'+countSw).change(function () {
                    if (form.find('.custSwitch_' + countSw + ':checked').length) {
                        form.find('.custAction_' + countSw).prop('disabled', false).trigger("chosen:updated").trigger("change");
                    } else {
                        form.find('.custAction_' + countSw).prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
                    }
                }); countSw++
            }
            form.find('.custSwitch_4').change(function () {
                if (form.find('.custSwitch_4:checked').length) {
                    form.find('.custAction_4').prop('disabled', false).trigger("chosen:updated").trigger("change");
                    form.find('.custAction_4b').button("enable");
                } else {
                    form.find('.custAction_4').prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
                    form.find('.custAction_4b').prop("checked", false).button("refresh").button("disable", "disable");

                }
            });
        });
    });
$(函数(){
$('form')。每个(函数(){
变量形式=$(此);
var switchClass=$('form[class^=“custSwitch_quo;]).length;
form.find('[class^=“custAction”]').prop('disabled',true.).trigger(“selected:updated”);
form.find('[class^=“custAction_4b”]')。按钮('disable');
var countSw=1;
while(countSw
但是,唉,它没有!:-)我知道我遗漏了一些东西,可能是一些简单的东西。 .custSwitch_1、2和3类不再启用和禁用custAction_1、2和3类


Si.

代码的问题是变量
countSw
始终为3,因为执行
change
侦听器中的代码时,变量的值为3

while (countSw < switchClass) {
  form.find('.custSwitch_'+countSw).change(function () {
    doSomething(countSw);
  }); countSw++
}

function doSomething(countSw) {
  if (form.find('.custSwitch_' + countSw + ':checked').length) {
    form.find('.custAction_' + countSw).prop('disabled', false).trigger("chosen:updated").trigger("change");
  } else {
    form.find('.custAction_' + countSw).prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
  }                    
}
while(countSw

请注意,您的
countSw
现在作为参数传递。

现在是一个明显的问题。。。我该如何改变这一点?:-)正如你可能知道的,我不是职业选手!:-)您可以在使用我的函数时切换第一个函数,然后在主函数(窗体循环)之外编写我编写的函数。它应该会起作用。谢谢你的帮助,我知道帮助新手对你的Java天才来说可能很乏味。:-)