2014-09-26 36 views
1

默認情況下,JQueryUI的自動完成將空格編碼爲「+」。我的遠程源期望編碼爲「%20」。是否有可能改變空間編碼的方式?可能更改JQueryUI自動完成的空間編碼?

+0

一個可以玩的例子會很棒。你可以在http://jsfiddle.net發佈一個例子 – 2014-09-29 17:54:20

+0

@Vega:默認情況下,並不確定它需要一個,因爲JQuery-UI的自動完成將空格編碼爲'+'。 – 2014-09-29 18:28:02

+2

如果您使用'source:「http://example.com/」',嘗試使用'source:function(request,successCallback){$ .ajax(...)。done( successCallback); }'。 – Regent 2014-09-30 10:36:14

回答

1

的理念是:

  • 使自動完成請求到服務器使用手動$.ajax()
  • 手工編寫通過替換髮送的數據「」與「%20」
  • 以防止發送的數據的默認處理使用processData: false

Example fiddle

<input id="test"> 

$('#test').autocomplete 
({ 
    source: autocompleteRequest 
}); 

function autocompleteRequest(request, response) 
{ 
    var inputString = request.term; 
    var preparedString = inputString.replace(/\s/g, "%20"); 
    $.ajax({ 
     url: "test/", 
     data: "str=" + preparedString, 
     processData: false 
    }).success(function(data) 
    { 
     response(data); 
    }); 
} 
0

我無法複製你的問題,但作爲ManoNamo說,你的問題很可能是現在的樣子您將數據發送到您的服務器。 encodeURI和encodeURIComponent都適合您的目的。看到一個工作示例如下:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>jQuery UI Autocomplete - Default functionality</title> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css"> 
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
    <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script> 
    <link rel="stylesheet" href="http://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css"> 
    <script> 
    $(function() { 
    var availableTags = [ 
     "ActionScript KABAM", 
     "ActionScript", 
    ]; 
    $("#tags").autocomplete({ 
     source: availableTags, 
     change: function(event, ui) { 
     console.dir(encodeURIComponent(ui.item.value)); 
     console.dir(encodeURI($("input#tags").val())); 
     } 
    }); 
    }); 
    </script> 
</head> 
<body> 

<div class="ui-widget"> 
    <label for="tags">Tags: </label> 
    <input id="tags"> 
</div> 


</body> 
</html> 
+0

這最終將雙重編碼的事物和「邁克爾瓦倫丁」結束爲「邁克爾%2520Valentine」 – 2014-10-03 21:48:12