1
我有這件jQuery,我知道它是重複的。 我需要一些幫助來清理這個,我還是jQuery/JavaScript的新手!有沒有辦法縮短這個jQuery?
$(function() {
$('form').each(function() {
var form = $(this);
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, 'value':''}).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, 'value':''}).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, 'value':''}).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");
} else {
form.find('.custAction_4').prop({'disabled': true, 'selectedIndex': 0, 'value':''}).trigger("chosen:updated").trigger("change");
}
});
form.find('.roof').change(function() {
if (form.find('.roof:checked').length) {
form.find('.sunroof').prop('disabled', false).trigger("chosen:updated").trigger("change");
form.find('.antenna').button("enable");
} else {
form.find('.sunroof').prop({'disabled': true, 'selectedIndex': 0}).trigger("chosen:updated").trigger("change");
form.find('.antenna').prop("checked", false).button("refresh").button("disable", "disable");
}
});
});
});
我不知道是否使用循環,或像
className.replace("custAction_", "custSwitch")
以及如何實現它.....一直在尋找了兩天,現在,正好可以」似乎要麼讓我的頭在附近!
任何幫助,將不勝感激。
您可能會發現[CodeReview](http://codereview.stackexchange.com/)網站對這類問題有幫助。 – Spudley
請注意,如果您確實將此內容發佈到Code Review,則標題應該具有代碼所做的描述,而不僅僅是想要改進。 –