我正在提取當前按鈕ID的一部分,並希望它在其他幾個函數中重用。在JavaScript中創建可重用函數
我如何才能讓下面的部分常見,
var idRec = $(this).attr("id"),
inp = idRec.substr(4,this.id.length);
,以便它可以在多個.on('click',)
活動中使用。請看下面的代碼,並建議,
$(function() {
function studentsRecords(e) {
//do_somehting
}
$(document).on('click', '.studentID', function(e) {
var idRec = $(this).attr("id"),
inp = idRec.substr(2, this.id.length);
//do_something_using_inp
}).on('click', '.admissionID', function(e) {
//do_something_else_using_inp
});
});
只是刪除關鍵字'var'並添加'變種idRec,INP;''之前$(文件)。在('click'' – Regent 2014-09-03 05:43:19
兩次上點擊就像你有錯... – 2014-09-03 05:43:24