简体   繁体   English

select 中最接近的匹配项

[英]Closest match in a select

I have a select element containing different titles;我有一个包含不同标题的select元素; as an example:举个例子:

<select name="titles">
  <option value="1">Mr.</option>
  <option value="2">Mrs.</option>
  <option value="3">Ms.</option>
  <option value="4">Dr.</option>
  [..]
</select>

Then I have a string that contains a user-submitted title (originally written in a freeform textbox).然后我有一个包含用户提交的标题的字符串(最初写在一个自由格式的文本框中)。 My task is to make selected the option in the select that corresponds to that title.我的任务是选择select中与该标题对应的option

However, it occurs to me that users are sometimes stupid.然而,我发现用户有时是愚蠢的。

The user-supplied string I search with could be "dr."我搜索的用户提供的字符串可能是“dr”。 or "Dr" or something like that.或“博士”或类似的东西。 I need to match it with the most relevant one (ie "Dr." instead of "Mr.", both of them close to say, "dr.").我需要将它与最相关的匹配(即“Dr.”而不是“Mr.”,两者都接近于说“dr.”)。

How would I go about this?我怎么会go这个呢? I've only ever done approximate matching with MySQL's LIKE and PHP's levenshtein() , neither of which to my understanding are rather relevant in JS.我只用 MySQL 的LIKE和 PHP 的levenshtein()做过近似匹配,据我所知,这两者都与 JS 无关。

jQuery 1.7.1 is available. jQuery 1.7.1 可用。 IE6 compatibility doesn't concern me. IE6 兼容性与我无关。

Thank you in advance!先感谢您!

If I'm not mistaken, the only reasonable (and acceptable) variations are capital letter / no capital letter at the beginning and a missing point character at the end.如果我没记错的话,唯一合理(和可接受的)变化是开头的大写字母/没有大写字母和末尾的缺失点字符。

If that's the case then you can simply compare the user string to each of the items in the select with the above in mind:如果是这种情况,那么您只需将用户字符串与select中的每个项目进行比较,并牢记上述内容:

  • Convert first character of user input to uppercase.将用户输入的第一个字符转换为大写。
  • Add a .添加一个. to the end of the string if the user string does not end with .如果用户字符串不以 . 结尾,则到字符串的末尾. . .
  • Compare the modified user string with each of the elements in select and see if one matches.将修改后的用户字符串与select中的每个元素进行比较,看看是否匹配。

The users are never stupid;)用户永远不会傻;)

Before you do the matching I would first make transform my all letters to lowecase in my javascript, then i would look for dots (.) and mabye remove them or add them to you match-string (its your choise).在你进行匹配之前,我首先会在我的 javascript 中将我的所有字母转换为小写字母,然后我会寻找点 (.) 并可能将它们删除或将它们添加到你的匹配字符串(你的选择)。

Hope that helps.希望有所帮助。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <!-- Date: 2012-04-05 -->
    <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        var title = "dr"
        $("#titles option").each(function(i, el) {
            var re = new RegExp(title, 'ig')
            if ($(el).html().match(re)) {
                $(el).attr("selected", "selected");
            }
        });
    })
    </script>
</head>
<body>
    <select name="titles" id="titles">
      <option value="1">Mr.</option>
      <option value="2">Mrs.</option>
      <option value="3">Ms.</option>
      <option value="4">Dr.</option>
    </select>
</body>
</html>

And another solution to your issue - give your users exactly this form so they will have no chance to type in any wrong data.另一个解决您问题的方法 - 为您的用户提供准确的表格,这样他们就没有机会输入任何错误的数据。

Would you consider stripping the extra layer of matching a freeform string to a list item?您会考虑剥离将自由格式字符串与列表项匹配的额外层吗? Maybe have an autocomplete field/dropdown or similar;也许有一个自动完成字段/下拉列表或类似的; seems excessive to have user enter and re-select the same data twice.让用户输入并重新选择相同的数据两次似乎太过分了。

I was going to suggest looking into a fuzzy search algorithm, but wouldn't it be simpler to have an array of values that match a particular title?我本来打算建议研究一种模糊搜索算法,但是拥有一组与特定标题匹配的值不是更简单吗? It could be than compared with normalized periods, capitalization and whitespaces, ie它可以与规范化的句点、大写和空格进行比较,即

  • user-entered Dr , dr.用户输入的Dr , dr. and Dr. would be normalized and matched against Dr.并且Dr.将被归一化并与Dr.匹配。
  • user-entered doctor and Doctor would be normalized and matched用户输入的doctorDoctor将被标准化和匹配
  • user-entered doc , Doc.用户输入的docDoc. and doc would be normalized and matched etc.并且doc将被规范化和匹配等。

This sounds like a mind-numbing task though going through variations of misters etc. Really consider making it into a normalized dropdown in the first place;这听起来像是一项令人麻木的任务,尽管要经历各种各样的先生等等。真正考虑首先将其纳入规范化的下拉列表; the only thing that would be harder to process than this would be a non-normalized address field:)唯一比这更难处理的是非规范化地址字段:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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