我正在提取当前按钮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