2011-07-30 27 views
0

我的CMS有大約30個不同的模塊,下面是代碼包含在每個模塊的javascript中,除了一些更改外,並不知道是否有更簡單的方法或我可以做的更多,以消除必須反覆複製相同的代碼,以防萬一我必須回去改變一些東西,我只能做一次。但頁面上不同的部分是任何說每個頁面上的單詞模板不同的部分,因爲它表示模塊化名稱。我不知道我是否可以改變東西像itemID和其他任何東西。將所有文件的類似代碼更改爲一個

$('.ask').jConfirmAction({ 
    question : "Are you sure you want to delete the selected row?", 
    yesAnswer : "Yes", 
    cancelAnswer : "No", 
    onYes: function(evt) { 
     templates(evt.target); 
    } 
}); 

$('.ask2').jConfirmAction({ 
    question : "Are you sure you want to delete all selected rows?", 
    questionClass: "question2", 
    onYes: function(evt){ 
     templatesArray(evt.target); 
    } 
}); 

function templates(whatsThis) { 
    var templateID = $(whatsThis).parents('td').find('img').attr('id'); 
    var dataString = 'templateID=' + templateID + '&deleteTemplate=True'; 

    var iRow = oTable.fnGetPosition($(whatsThis).parents('tr').get(0)); 

    $.ajax({ 
     type: "POST", 
     url: "processes/templates.php", 
     data: dataString, 
     success: function(data) { 
      if (data.errorsExist) { 
      } else { 
       oTable.fnDeleteRow(iRow);  // remove the row from the table 
       if(oTable.fnSettings().fnRecordsTotal() == 0) { 
        $('.bt_red').remove(); 
        $('.bt_blue').remove(); 
       } 
       if(oTable.fnSettings().fnRecordsTotal() <= 10) { 
        $('.bt_blue').remove(); 
       } 
      } 
     } 
    }); 
} 


function templatesArray(whatsThis) { 
    var myNewArray = new Array(); 
    var aRow = new Array(); 

    $('input:checkbox[name="templates"]:checked').each(function(i) { 
     myNewArray.push($(this).val()); 
     aRow.push($(this).closest('tr')[0]); 
    }); 
    var dataString = 'templatesArray=' + myNewArray + '&deleteTemplatesArray=True'; 
    $.ajax({ 
     type: "POST", 
     url: "processes/templates.php", 
     data: dataString, 
     success: function(data) { 
      if (data.errorsExist) { 
      } else { 
       $(whatsThis).parents("tr").eq(0).hide(); 
       for (i in aRow) // loop over the array of row indexes 
        oTable.fnDeleteRow(aRow[i]); 
       if(oTable.fnSettings().fnRecordsTotal() == 0) { 
        $('.bt_red').remove(); 
        $('.bt_blue').remove(); 
       } 
       if(oTable.fnSettings().fnRecordsTotal() <= 10) { 
        $('.bt_blue').remove(); 
       }      
      } 
     } 
    }); 

} 
+0

爲什麼我的帖子編輯? –

+1

jquery標籤被添加,因爲你有jQuery代碼。 – Mrchief

回答

1

的代碼做不同的事情,訪問不同的對象onYes。我不認爲它可以結合。也許一些simialr的其他功能可能不是發佈的功能。

相關問題