2015-06-23 29 views
5

我有這個工作代碼波紋管,我認爲他是有點太長,冗餘,我可以定製它?自定義冗餘複選框輸入Jquery代碼

$("#unique").click(function() { 

    if ($(this).is(':checked')) { 
     $(".lotud").show(); 
     $("#add_lot").hide(); 
     $("#lots_rows_contnr").hide(); 

     $(".lotud input").prop({disabled: false}) 
     $("#lots_rows_contnr input").prop({disabled: true}) 
    } 
    else { 
     $(".lotud").hide(); 
     $("#add_lot").show(); 
     $("#lots_rows_contnr").show(); 

     $(".lotud input").prop({disabled: true}) 
     $("#lots_rows_contnr input").prop({disabled: false}) 
    } 

}); 

回答

6

,可以稍微縮短它通過使用ternaries,用在一個DOMElement本身checked財產,加盟選擇和使用checked財產作爲財產disabled的基礎。試試這個:

$("#unique").click(function() { 
    $(".lotud").toggle(this.checked); 
    $("#add_lot, #lots_rows_contnr").toggle(!this.checked); 
    $(".lotud input").prop({ disabled: !this.checked }); 
    $("#lots_rows_contnr input").prop({ disabled: this.checked }); 
}); 

您的原始或上述兩個版本中哪一個更具可讀性是一個意見問題。

+0

謝謝@Rory我現在忍受你的接近;)我也不明白爲什麼downvote,因爲我有一個工作代碼,我只想定製它 – Youssef