2016-04-08 3 views
1

を発射ない、イベントが最初に次のようになります。それはそのようなものだときjQueryの、からkeyupイベントは、私は私が私のシステムを持っているから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(""); 
     } 
    }; 

それは動作を停止し、n個何かを入力すると何かが起こります。コンソールにはエラーが表示されないので、searchClient関数の先頭にconsole.logを書いて、ページが読み込まれたときにfireが発生し、params.social_reasonに空の文字列が含まれていることが示されます。私は言った、何も起こらない。私は行方不明を知りません、私がしたのは、イベントのコードをsearchClient関数にコピー/ペーストし、var paramsを削除することです(パラメータとして受け取るため)。

+1

同じ答え別の質問を。私はhttp://stackoverflow.com/q/8462381/497418または他の多くの関連する質問を使用することを検討しましたが、主に 'setTimeout'関連です。結局、同じ基本的な問題。 – zzzzBov

答えて

1

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(""); 
    } 
}; 
+0

興味深いことに、同じ機能を別の要素に追加したいのですが、異なるparams値を使用したいので、問題は私がコードブロック内にあることを望まないということです。私は他の方法でそれをしなければならないと思う。 – Dukra

関連する問題