大家好,我制作了这个事件是为了知道何时按下按钮进入,但如果我尝试使用绑定或它不起作用。请问有什么帮助吗?
这是我的活动:
$.fn.enterKey = function (fnc) {
return this.each(function () {
$(this).keypress(function (ev) {
var keycode = (ev.keyCode ? ev.keyCode : ev.which);
if (keycode == '13') {
fnc.call(this, ev);
}
})
})
}
如果我尝试这样做:
$('.foo').on('enterKey', function() {
// other code --> doesn't work
});
$('.foo').bind('enterKey', function() {
// other code --> doesn't work
});
这是我的代码:
var add_comment = { // add comment
urlRemove : CI_ROOT + 'add_comment_team/',
run : function() {
add_comment.share('.comment');
},
share : function(obj) {
var alreadySent = false;
$(obj).enterKey(function() {
var comment = $(this);
if (comment.val() != "") {
if(!alreadySent) {
alreadySent = true;
$.ajax({
type: "POST",
url: add_comment.urlRemove,
data: "text=" + comment.val() + "&post_id=" + comment.attr('id'),
success: function(html) {
comment.val('');
comment.closest('.shared_box').children('.recent_comment').append(html);
alreadySent = false;
},
error: function(){
alert('Error on ajax call');
alreadySent = false;
}
});
}
} else {
return false;
}
});
}
};
我试图用触发器这样做,但仍然无法正常工作
var add_comment = { // add comment
urlRemove : CI_ROOT + 'add_comment_team/',
run : function() {
add_comment.share('.comment');
},
share : function(obj) {
var alreadySent = false;
$(document).on('enterKey',obj, function() {
var comment = $(this);
if (comment.val() != "") {
if(!alreadySent) {
alreadySent = true;
$.ajax({
type: "POST",
url: add_comment.urlRemove,
data: "text=" + comment.val() + "&post_id=" + comment.attr('id'),
success: function(html) {
comment.val('');
comment.closest('.shared_box').children('.recent_comment').append(html);
alreadySent = false;
},
error: function(){
alert('Error on ajax call');
alreadySent = false;
}
});
}
} else {
return false;
}
});
$(obj).trigger('enterKey');
}
};