您可以尝试使用 jQuery .on 方法,
$("table").on("keypress", "input", function(event){
alert($(this).attr("id"));// gets the input id
alert($(this).val());// gets the input value
});
此代码将处理<table>
标签内的所有输入。
如果您不想在每次击键时都执行此侦听器,请给一些时间(3 秒)呼吸,请尝试以下代码 -
var timeoutReference;
$("table").on("keypress", "input", function(event){
var el = this; // copy of this object for further usage
if (timeoutReference) clearTimeout(timeoutReference);
timeoutReference = setTimeout(function() {
doneTyping.call(el);
}, 3000);
});
$("table").on("blur", "input", function(event){
doneTyping.call(this);
});
function doneTyping(){
var el = this;
// we only want to execute if a timer is pending
if (!timeoutReference){
return;
}
// reset the timeout then continue on with the code
timeoutReference = null;
//
// Code to execute here
//
alert('This was executed when the user is done typing.');
alert($(el).attr("id"));//id
alert($(el).val());//value
}