我可以按照以下方式拥有多个实例,或者我应该拥有多个实例,但是确定触发事件和处理的内容是什么?
$('.someClass')
.on("keydown","[id*=txtAccountID],[id*=txtLinkedToAccountID]...
$('.someClass')
.on("keydown","[id*=txtDateDB],[id*=txtDateHuman]...
OR
$('.someClass')
.on("keydown","[id*=txtAccountID],[id*=txtLinkedToAccountID],[id*=txtDateDB],[id*=txtDateHuman]...
resID = $(this).prop("id");
if (resID = "XYZ" ) {
//do account related stuff
else if (resID = "ABC" ) {
//do date related stuff
}
答案 0 :(得分:5)
是的,你可以。根据{{3}}:
如果注册了多个处理程序,它们将始终执行 他们被约束的顺序
修改强>
此qoute来自.bind
文档,但也与.on
相关
您也可以在.on
DOCS中看到相同的方案。
从jQuery 1.4开始,可以将相同的事件处理程序绑定到一个元素 多次。
示例:
$('#parent').on('click','.child, .child3', function(e){ console.log("Click #1 for - " + e.target.innerText)})
$('#parent').on('click','.child, .child3', function(e){ console.log("Click #2 for - " + e.target.innerText)})

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div id="parent">
<div class="child child1">
child 1 ( click me!)
</div>
<div class="child child2">
child 2 ( click me!)
</div>
<div class="child3">
child 3 ( click me!)
</div>
</div>
&#13;
答案 1 :(得分:1)
您可以根据需要将任意数量的事件处理程序绑定到元素
$(function(){
var reset = $("#reset");
var run1 = $("#run1");
var run2 = $("#run2");
var target = $("#target");
var defaultText ="Click the buttons To see multiple handlers evaluated in the order they were bound";
reset.click(rst);
var log = $("#data");
function rst(ev){
target.text("Event Triggered by "+ev.target.id);
log.text(defaultText);
}
function clear(ev){
target.text("Event Triggered by "+ev.target.id);
log.text("");
}
function one(ev){
target.text("Event Triggered by "+ev.target.id);
$('<p>One</p>').appendTo(log);
}
function two(ev){
target.text("Event Triggered by "+ev.target.id);
$('<p>Two</p>').appendTo(log);
}
function three(ev){
target.text("Event Triggered by "+ev.target.id);
$('<p>Three</p>').appendTo(log);
}
run1.click(clear)
.click(one)
.click(two)
.click(three);
run2.click(clear)
.click(one)
.click(two)
.click(three);
});
.fun {
width:100px;
text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="fun" id="run1">Run - 1</button>
<button class="fun" id="run2">Run - 2</button>
<button class="fun" id="reset">Reset</button>
<div id="target"></div>
<div id="data">
Click the buttons
To see multiple handlers
evaluated in the order
they were bound
</div>