jQuery 自动完成验证选定的值 [英] jQuery Autocomplete verify selected value

查看:13
本文介绍了jQuery 自动完成验证选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用由 Jörn Zaefferer 编写的自动完成 jQuery 插件,并且正在尝试验证输入了有效的选项.

We are using the autocomplete jQuery plugin written by Jörn Zaefferer, and are trying to verify a valid option is entered.

该插件具有 result() 事件,在进行选择时会触发该事件.这没问题,但我也需要在用户单击时检查文本框中的值.所以我们尝试了 .change() 和 .blur() 事件,但它们都带来了一个问题:当您单击结果 div(建议"列表)中的条目时,.change() 和 .blur()事件触发,这是在插件将值写入文本框之前,因此此时无需验证.

The plugin has result() event which fires when a selection is made. This is OK, but I need to check the value in the textbox when a user clicks out too. So we have tried a .change() and .blur() events, but they both pose an issue: when you click on an entry in the results div (the 'suggest' list) the .change() and .blur() events fire, and this is before the plugin has written a value to the textbox, so there is nothing to verify at this point.

有人可以帮我配置事件,所以只要有人点击离开,但不在结果框中,我可以检查框中的有效值.如果这是错误的方法,请告诉我正确的方法.我们最初使用这个插件是因为它的mustMatch"选项.此选项似乎并非在所有情况下都有效.很多时候,一个有效的条目会被写入文本框,然后被插件清除为无效,我一直无法确定原因.

Could someone help me configure events so whenever someone clicks away, but not in the results box I can check for a valid value in the box. If this is the wrong approach please inform me of the correct one. We originally went with this plugin because of its 'mustMatch' option. This option doesn't seem to work in all cases. Many times a valid entry will be written into the textbox and then cleared by the plugin as invalid, I have not been able to determine why.

基本代码示例:

<html>
<head>
<title>Choose Favorite</title>
<script language="JavaScript" src="jquery-1.3.2.min.js" ></script>
<script language="JavaScript" src="jquery.autocomplete.min.js" ></script>
<script>
    $(".suggest").autocomplete("fetchNames.asp", {
        matchContains:false,
        minChars:1, 
        autoFill:false,
        mustMatch:false,
        cacheLength:20,
        max:20
    });

    $(".suggest").result(function(event, data, formatted) {
        var u = this;
        // Check value here

    });

    /* OR */

    $(".suggest").change(function(me) {
        //check value here
    });

</script>
</head>
<body>
    <label for="tbxName">Select name (I show 10):</label><br />
    <INPUT type="text" id="tbxName" name="tbxName" class="suggest"><br />
</body>
</html>

推荐答案

我认为与其编写自己的函数来验证数据是否匹配,不如调用 search().如果使用空 data 参数调用 result(),那么您知道没有使用自动完成功能,并且通过调用 search()在模糊时,您可以保证至少调用一次 result().

I think rather than writing your own function to verify if the data matches, you can just call search(). If result() is called with a null data parameter, then you know that auto-complete wasn't used and by calling search() on blur, you're guaranteed to get result() called at least once.

我已经为发布了此代码一个类似的问题,它可能对这里也有帮助.

I've posted this code for a similar question, it may help here as well.

autocompleteField.result(function(event, data, formatted) {
    if (data) {
        //auto-complete matched
        //NB: this might get called twice, but that's okay
    }
    else {
        //must have been triggered by search() below
        //there was no match
    }
});

autocompleteField.blur(function(){
    autocompleteField.search(); //trigger result() on blur, even if autocomplete wasn't used
});

这篇关于jQuery 自动完成验证选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆