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

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

问题描述

我们正在使用JörnZefferer 编写的自动完成jQuery插件,并尝试验证输入有效选项。



插件具有在进行选择时触发的result()事件。这是可以的,但是当用户也点击时,我需要检查文本框中的值。所以我们尝试了一个.change()和.blur()事件,但是它们都是一个问题:当你点击结果div(建议列表)中的一个条目。.change()和.blur()事件触发,这是插件之前已经写了一个值到文本框,所以没有什么可以验证在这一点上。



有人可以帮助我配置事件,以便每当有人点击,而不是在结果框中,我可以检查框中的有效值。如果这是错误的方法,请通知我正确的。我们最初使用这个插件,因为它的mustMatch选项。在所有情况下,此选项似乎都不起作用。很多时候,一个有效的条目将被写入文本框,而不是由插件清除为无效,我无法确定为什么。



基本代码示例: p>

 < html> 
< head>
< title>选择收藏夹< / title>
< script language =JavaScriptsrc =jquery-1.3.2.min.js>< / script>
< script language =JavaScriptsrc =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>选择名称(我显示10):< / label>< br />
< INPUT type =textid =tbxNamename =tbxNameclass =suggest>< br />
< / body>
< / html>


解决方案

我认为,而不是编写自己的函数来验证数据匹配,您可以调用 search()。如果 result()被调用一个null 数据参数,那么你知道没有使用自动完成调用 search()在模糊,你保证得到 result()至少调用一次。 p>

我已经发布了代码类似的问题,这可能也有帮助。

  autocompleteField .result(function(event,data,formatted){
if(data){
//自动完成匹配
// NB:这可能会被调用两次,但没关系
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $



}

autocompleteField.blur(function(){
autocompleteField.search(); //触发结果()模糊,即使没有使用自动填充
});


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

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.

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 than cleared by the plugin as invalid, I have not been able to determin why.

Basic Code example:

<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>

解决方案

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天全站免登陆