2014-10-18 44 views
-1

我有一個輸入字段,在ajax響應中,我在li標籤中獲得了一些記錄。每一個li上都有一個onclick函數add_values。我想輸入鍵將automaticcly按下時任李標籤 在ajax響應後自動按回車鍵

function key_up_val(val) { 
      if(val.length > 5) { 
       $.ajax({ 
        url:'ajax/google_suggestion.php', 
        data:'keyword='+val, // KEYWORD PASSED VALUE WILL GO TO google_suggestion.php 
        success:function(res){ 
         if(res != '') { 
          $(".suggestion_div").show(); 
          $("#suggestion").html(res).fadeIn(); 
         } else { 
          $(".suggestion_div").hide(); 
          $("#suggestion").hide(); 
         } 
         //alert(res); // RESPONSE 
        } 
       }); 
      } 
     } 

function add_val(values){ // this function is used in the ajax request onclick event 
     $("#Search").val(values); // add the clicked suggestion the the searched field 
     //clickevent(); 
    $(".txt_field").focus();  
    var e = $.Event('keyup'); 
    e.which = 13; // Character 'A' 
    $(".txt_field").trigger(jQuery.Event('keypress', {which: 13})); 

}

關於Ajax響應用戶點擊,我得到: -

<li class="tushar morzaria" onclick="add_val('tushar morzaria')">tushar morzaria</li> 
<li class="tushar meaning" onclick="add_val('tushar meaning')">tushar meaning</li> 

我要自動按當用戶點擊任何li標籤時在文本欄中輸入

+1

on'li.onclick'你可不可以運行同樣的代碼,當你按下「Enter」鍵時運行? – akonsu 2014-10-18 13:22:57

+0

對於進入按,我想做警報框 – 2014-10-18 13:27:47

回答

0

最後我得到了解決方案。

function add_val(values){ 
    $("#Search").val(values); // add the clicked suggestion the the searched field 
    $(".txt_field").focus();  
    var e = jQuery.Event("keydown", { keyCode: 13 }); 
    $(".search_field input").trigger(e); 
} 

這將自動按回車上L1標籤點擊關鍵。

謝謝大家