我在重构系统中的 keyup
事件时遇到问题,该事件最初如下所示:
$('#quotation_search_client').on('keyup',function(){
param=$('#quotation_search_client').val();
if(param.length>=4){
var params = {
social_reason: $('#quotation_search_client').val()
}
var clients=ClientServices.getByFilter(params);
var addresses=ClientServices.getAddresses(clients.clients);
QuotationServices.fillClients(clients.clients, addresses);
}else{
$('#customers_results').html("");
}
});
当它是这样的时候,它工作得很好,但是当我将其更改为这样时:
$('#quotation_search_client').on('keyup',QuotationServices.searchClient({social_reason: $('#quotation_search_client').val()}));
并像这样定义函数“searchClient”:
QuotationServices.searchClient = function(params){
param=$('#quotation_search_client').val();
if(param.length>=4){
var clients=ClientServices.getByFilter(params);
var addresses=ClientServices.getAddresses(clients.clients);
QuotationServices.fillClients(clients.clients, addresses);
}else{
$('#customers_results').html("");
}
};
它停止工作,当我输入内容时没有任何反应。控制台没有显示任何错误,所以我在 searchClient
函数的开头编写了一个 console.log ,显然它在我的页面加载时触发,显示 params.social_reason
带有一个空字符串,但是当我输入一些内容时,正如我所说, 什么都没发生。我不知道我错过了什么,我所做的只是将事件的代码复制/粘贴到 searchClient
函数中并删除 var params
(因为我将收到它作为参数),有什么帮助吗?
最佳答案
您在声明 keyup 事件后立即执行该函数,因此您没有将该函数传递给 keyup 事件。
你应该这样做:
$('#quotation_search_client').on('keyup',QuotationServices.searchClient);
并更改:
QuotationServices.searchClient = function(){
var params = {social_reason: $('#quotation_search_client').val()}
var param = $('#quotation_search_client').val();
if(param.length>=4){
var clients=ClientServices.getByFilter(params);
var addresses=ClientServices.getAddresses(clients.clients);
QuotationServices.fillClients(clients.clients, addresses);
}else{
$('#customers_results').html("");
}
};
关于javascript - jQuery,keyup 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36508441/