2012-11-08 66 views
4

我已經實現了使用JQuery的自動完成功能,當我選擇數據時,它被存儲在結果表中,選擇後,我需要清除自動完成的文本框,我無法。搜索了相同的並得到了這個Clear text box in Jquery Autocomplete after selection,但我不知道該把它放在哪裏以及在螢火蟲中,我在函數(事件,UI)中出錯。 請幫助...我的代碼如下。自動完成後清除文本框在jquery中選擇

$(function() { 
    function log(message) { 


     $("<div>").text(message).prependTo("#log").click(function(o){ 
      $(this).remove(); 
     }); 
     $("#log").scrollTop(0); 

    } 


    $("#poolName").autocomplete({ 
     source: function(request, response) { 
      $.ajax({ 
       url: "/DataWeb/getPoolName", 
       type : 'post', 
       dataType: 'json', 
       data: { name_startsWith: request.term }, 
       success: function(data) { 
        console.log(data); 
        response($.map(data, function(item) { 
         return { 
          label: item.poolName, 
          value: item.poolName 
         } 
        })); 
       } 
      }); 
     }, 
     minLength: 1, 
     select: function(event, ui) { 
      log(ui.item ? 
        ui.item.label : 
       "Nothing selected, input was " + this.value); 
     }, 
     open: function() { 
      $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
     }, 
     close: function() { 
      $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
     } 

    }); 

}); 

回答

1

我試圖與此的document.getElementById( 「POOLNAME」)值= 「」;。它正在工作。

function log(message) { 
     document.getElementById("poolName").value=""; 

     $("<div>").text(message).prependTo("#log").click(function(o){ 
      $(this).remove(); 
     }); 
     $("#log").scrollTop(0); 

    } 
2

嘗試使用return false;內選擇:子句,

select: function(event, ui) { 
      log(ui.item ? 
        ui.item.label : 
       "Nothing selected, input was " + this.value); 
      return false; 
     }, 
+0

它沒有工作.. – madhu

+0

什麼是你得到了錯誤螢火蟲 – Swarne27

+0

它適用於我,但爲什麼?我不知道作爲一個JavaScript新手。 – kxxoling

3
$("#poolName").autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
      url: "/DataWeb/getPoolName", 
      type : 'post', 
      dataType: 'json', 
      data: { name_startsWith: request.term }, 
      success: function(data) { 
       console.log(data); 
       response($.map(data, function(item) { 
        return { 
         label: item.poolName, 
         value: item.poolName 
        } 
       })); 
      } 
     }); 
    }, 
    select: function (e, i) { 
     $('#poolName').val(''); 
     return false;  
    } 

    ,minLength: 1, 
    select: function(event, ui) { 
     log(ui.item ? 
       ui.item.label : 
      "Nothing selected, input was " + this.value); 
    }, 
    open: function() { 
     $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
    }, 
    close: function() { 
     $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
    } 

}); 

}); 
+0

它沒有工作.. – madhu

+0

你可以粘貼你嘗試的代碼或創建一個jsfiddle(ttp://jsfiddle.net/),以便我們可以檢查你的代碼 –

+0

你嘗試這個時得到的任何錯誤。 –

0

與其他答案的問題是,它沒有preventDefault調用。

這將工作:

select: function (e, i) { 
    e.preventDefault(); 
    $('#poolName').val(''); 
    return false;  
} 
0

我發現,我不得不這樣做既清場並返回false

select: function(ev, ui) { 
    console.log(this, ui.item) 
    $(this).val('') 
    return false 
},