2011-03-15 11 views
0

共有8個按鍵一個.attr(「禁用..)我有8個不同的<form> S IN同一頁面,用戶只能編輯一個,保存或取消,編輯另一個,保存或取消,等我有很多重複的代碼,因爲有些問題來處理它舉例來說,對於第一種形式:如何簡單地在jQuery的?

/* When the button EDIT is clicked */ 
$("#project_title_edit").click(function(){ 
    $("#ajax-loader").fadeIn('normal'); 

    // @todo disable all the other project_XXX_edit 

    $(this).fadeOut(); 

    $("div#project_title").slideUp("600"); 

    $("div#project_title_form").delay("500").slideDown("600"); 

    window.onbeforeunload = function() { 
     return ''; 
    }; 

    $("#ajax-loader").fadeOut('normal'); 

    return false;  
}); 

/* When the button cancel, that appears inside the from, is clicked */ 
$("#project_title_cancel").click(function(){ 
    $("#ajax-loader").fadeIn('normal'); 

    // @todo enable all the other project_XXX_edit 

    window.onbeforeunload = null;  

    $("#project_title_edit").fadeIn(); 

    $("div#project_title_form").slideUp("600"); 

    $("div#project_title").delay("500").slideDown("600"); 

    $("#ajax-loader").fadeOut('normal'); 

    return false; 
}); 

這是我已經爲所有形式的模式,和很多重複代碼:以上x 8次我想知道這是否可以簡化,如果我已經,例如:

  • project_importance (project_importance_edit,project_importance_cancel,project_importance_form)
  • project_description(project_description_edit,project_description_cancel,project_description_form)

我需要另一件事是如何禁用所有xxx_edit按鈕,並重新啓用它。

預先感謝您!

回答

0

給按鈕類和你的函數綁定到類的Click事件。類似於

$('.edit').click(function() { 
    //edit code goes here 
}); 


$('.cancel').click(functon() { 
    //Cancel code goes here 
}); 

您可以刪除-edit並添加表單,例如

var form = $(this).id.replace('edit', '')+'form'