有两种情况需要检测行何时失去焦点,一种是在表格内,另一种是在离开表格时。
你可以尝试这样的事情:
//store the last visited row
var row = false;
// save the row if has changed
function save () {
if (row.changed){
console.log("save");
}
}
// keep track of the row you are in
// it doesnt work when you leave the table
$("tr").on("focusin", function (e) {
if (row != this){
if (row){
save();
}
row = this;
e.stopPropagation();
}
});
//keep track whenever the row changes
$("tr").on("change", function (e) {
this.changed = true;
console.log("changed");
})
//triggers when you leave the table, you can try to save changes then.
$(document).on("focusin", function (e) {
var el = $(e.target); //input or element that triggers this event
var elrow = el.parent().parent()[0]; // try to get the row from that input, ... if its an input
if (row && row !=elrow) {
save();
e.stopPropagation();
};
})