dougan6402 2015-06-18 06:25
浏览 92
已采纳

jQuery自动完成将查询转换为小写

I am using jQuery Autocomplete plugin 1.1 for a project.

Here is my calling code:

$("#txtCombo").autocomplete("http://www.example.com/autocomplete.php", {
            autoFill: false,
            width: 550,
            minChars: 3,
            selectFirst: false
        });

The problem is that what ever I type in the autocomplete, it gets converted to lower case when it reaches the php file autocomplete.php

For e.g. if I search for "Smart Boy", the php file will receive "smart boy". This is good in a way because when I make a like search in the database for the query, I get all the matching results irrespective of the letter case.

But here is the problem, when I run this script for greek characters, for e.g. when I enter query "Μικρές Οικιακές Συσκευές", it is converted to "μικρές οικιακές συσκευές". Now when I use a like query as below:

"SELECT ID, NAME FROM CATEGORIES WHERE NAME LIKE '%".$query."%'"

I get nothing, because the database does not check for greek letters with case-insensitive search.

I tried using collation keyword like this:

"SELECT ID, NAME FROM CATEGORIES WHERE NAME LIKE '%".$query."%' COLLATE utf8_general_ci"

But I get this error: COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'latin1'

I understand that the charset of the tables is latin1 so it wont work with utf8 collation, so if I try using latin1_general_ci collation, it gives me this error:

Illegal mix of collations (utf8_general_ci,IMPLICIT) and (latin1_general_ci,EXPLICIT) for operation 'like'

Only if I can stop autocomplete by converting the query to lowercase, it would solve the problem pretty much.

Why is the plugin converting to lowercase anyway when I have not specified any such thing in the parameters to call?

Any ideas....

  • 写回答

2条回答 默认 最新

  • dongwang6837 2015-06-18 06:46
    关注

    I have finally found a solution by searching the file jquery.autocomplete.js for the word "lower". I found out that it converts search queries to lower-case if an option "matchCase" is not found in the call parameters, or if it is not set to true.

    So I edited my calling like this:

    $("#txtCombo").autocomplete("http://www.example.com/autocomplete.php", {
            autoFill: false,
            width: 550,
            minChars: 3,
            matchCase: true,
            selectFirst: false
        });
    

    And it worked! Now it does not convert the query to lower-case.

    I think posting a question it self clarifies new ways to look for a solution. I did not get the idea to look for the word "lower" in the plugin file itself before that. Even though the plugin file was minified, still I found this code in there:

    ... if(!options.matchCase)currentValue=currentValue.toLowerCase(); ...
    

    which gave me the idea why it could have been converting to lowercase.

    May be this answer will help someone with the same situation.

    Regards

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题