简体   繁体   中英

jQuery autocomplete special characters

I have an ajax call which is fetching a JSON representation of a value created by a php json_encode method:

["Montérégie","Montréal - North Shore ","Montréal - South Shore"]

The values are being harvested from a 'controller/ajax_autocomplete' by a jquery autocomplete box.

  $(function(){   $("#regions").autocomplete({               
            source: "controller/ajax_autocomplete",
            contentType: "application/json; charset=utf-8"
}                                                          
            });            
    }); 

All values are being corectly picked up by jQuery UI's ui-autocomplete but the special charaters are lost. Montréal become Montréal, Montérégie become Montérégie ...

The special characters are certainly destroyed during http transport because the problem goes away if I manualy copy the JSON table to jquery function.

    $(function(){   $("#regions").autocomplete({               
            contentType: "application/json; charset=utf-8",
            source: "["Montérégie","Montréal - North Shore ","Montréal - South Shore"]"
}                                                          
            });            
    }); 

Programmatically decoding the html entity works for text box value but the suggestion list still replaces the special characters with HTML entities

$(function(){   $("#regions").autocomplete({              
        source: "controller/ajax_autocomplete",
        select: function( event, ui ) {
                event.preventDefault();
                this.value = $('<div />').html(ui.item.value).text();
        }                                                                                                                                                                                            
        });            
});   

The solution would be to decode the HTML entities in suggestion list

you should use :

example:

var myData = ["Montérégie","Montréal - North Shore ","Montréal - South Shore"];
encodeURIComponent(JSON.stringify(myData))

我已经通过将ajax输出包装到php'html_entity_decode'方法中修复了服务器端的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM