2015-09-06 148 views
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") 

以及如何實現它.....一直在尋找了兩天,現在,正好可以」似乎要麼讓我的頭在附近!

任何幫助,將不勝感激。

+0

您可能會發現[CodeReview](http://codereview.stackexchange.com/)網站對這類問題有幫助。 – Spudley

+0

請注意,如果您確實將此內容發佈到Code Review,則標題應該具有代碼所做的描述,而不僅僅是想要改進。 –

回答

2

您可以對此使用循環。

$('form').each(function() { 
    var form = $(this); 
    for(var i = 1; i<= 4 ; i++){ 
     form.find('.custSwitch_' + i).change(function() { 
      if (form.find('.custSwitch_' + i + ':checked').length) { 
       form.find('.custAction_' + i).prop('disabled', false).trigger("chosen:updated").trigger("change"); 
      } else { 
       form.find('.custAction_' + i).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"); 
     } 
    }); 
}); 
0

感謝您的幫助,居然得到了一些咖啡:-)研製出這種解決方案

  var switches = [1, 2, 3, 4]; 
     $.each(switches, function(index, value) { 
      form.find('.custSwitch_' + value).change(function() { 
       if (form.find('.custSwitch_' + value + ':checked').length) { 
        form.find('.custAction_' + value).prop('disabled', false).trigger("chosen:updated").trigger("change"); 
       } else { 
        form.find('.custAction_' + value).prop({'disabled': true, 'selectedIndex': 0, 'value': ''}).trigger("chosen:updated").trigger("change"); 
       } 
      }); 
     }); 

不知道是否有一個更短的方式....但我現在很高興! :-)