我正在使用 jQuery 代码通过回车键触发输入字段。我在另一个 View 中使用了代码,它工作得很好。在新的观点中,不知何故它根本不起作用。 keyup 被触发但 .bind()
函数未被调用。
这是 HTML 触发器:
<div class="form-group">
<h3>Customers</h3>
<input class="form-control" id="searchNew" placeholder="Search" name="searchNew" autofocus="" value="@form("searchValue").value">
</div>
这是 jQuery 代码:
$('#searchNew').bind("enterKey", function(e){
console.log("Test2");
$.ajaxSetup({
beforeSend: function(){
$('.loading').fadeIn();
},
complete:function(){
$('.loading').fadeOut();
}
});
$.ajax({
type : 'POST',
url : '@routes.Application.searchLicensesAdmin()',
data : $('#search').serialize(),
success : function (data) {
$('#license-table').html(data);
return false;
},
error : function (data) {
console.log("Error");
}
});
return false;
});
$('#searchNew').keyup(function(e){
if(e.keyCode == 13){
console.log("Test1");
$(this).on("enterKey");
}
});
Test1 被触发并显示在控制台中,但 Test2 甚至根本没有被触发。所以问题不在于 ajax
调用,而在于 .bind()
。这可能是它不起作用的愚蠢原因,但我无法弄清楚。 jQuery 包含在两个文档中。
最佳答案
你需要 $(this).trigger('enterKey')
。
.bind()
和 .on()
做的几乎相同,尽管 .bind()
从版本 3 开始被弃用(所以不要使用它;))。
语句 $(this).on('enterKey')
只是注册了一个新的事件监听器,带有一个空的/未定义的回调
关于javascript - 带有 .bind() 的 jQuery .keyup() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520166/