我有这个代码,似乎无法在使用.delegate
后检索.keypress
函数上的特定ID的值。ID和其他内容实际上是通过AJAX调用动态生成的。
$(document).delegate('.edit_tr', 'click', function(){
var cont_det_id = $(this).attr('id');
$("#name_" + cont_det_id).hide(); //hide span
$("#input_name_" + cont_det_id).show(); //show editable box
//alert cont_det_id works good and gets correct ID
}).keypress(function(e){
if(e.which == 13) { //if statement for pressing enter button
var cont_det_id = $(this).attr('id');
//alert cont_det_id gets undefined
}
});
我使用的是我以前工作的旧模板,所以jQuery版本是1.7.x。有人有解决办法吗?谢谢。
Arun p Johny在评论中的建议对我很有用。所以我把它贴在这里,希望能对别人有所帮助。
$(document).delegate('.edit_tr', 'click', function(){
var cont_det_id = $(this).attr('id');
$("#name_" + cont_det_id).hide(); //hide span
$("#input_name_" + cont_det_id).show(); //show editable box
//alert cont_det_id works good and gets correct ID
}).delegate('.edit_tr', 'keypress', function (e) {
if(e.which == 13) { //if statement for pressing enter button
var cont_det_id = $(this).attr('id');
//alert cont_det_id good here
}
});