2011-03-15 3 views
0

私は8つの異なる<form>を同じページに配置して、ユーザーが編集、保存、私はので、繰り返しのコードの多くをしましたし、それを処理するためのいくつかの問題など、保存またはキャンセルたとえば、最初の形式のために:。jQueryの合計8つのボタンに対して.attr( "disabled ..")を使用する方法

/* 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)

そして、私が必要とする他の事はALL xxx_editボタンを無効にし、それを再度有効にする方法です。

ありがとうございます!

答えて

0

ボタンにクラスを与え、クラスのクリックイベントに関数をバインドします。何かのように

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


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

-editを削除してフォームを追加することができます。

var form = $(this).id.replace('edit', '')+'form' 
関連する問題