重置g:在空白时自动完成值

Resetting g:autocomplete values when blanked out

本文关键字:空白 重置      更新时间:2023-12-23

我在grails应用程序中使用richui g:autocomplete来处理一些时髦的自动完成字段(显然)。

它们通常看起来是这样的:-

<richui:autoComplete class="required" style="width:500px" name="autoLook[X].id" id="autoLook[X]" 
                value="" action="${createLinkTo('dir': 'form/searchAJAX')}" 
                forceSelection="true" maxResultsDisplayed="20" minQueryLength="3" onItemSelect="updateHiddenInput(id, X, 'forms')" />

当用户清空字段时,我正试图找出如何强制他们重新插入之前的值。我有一个函数,当用户选择一个值(updateHiddenInput)时会调用它,但当字段被清空时,它不会触发。

有什么方法可以实现这一点吗?或者我需要实现某种.change()javascript(这对我来说有点垃圾)。。。?

我尝试添加以下内容,但似乎不起作用:-

$(function() {
    $(document).on('change', 'input', function() {
        var formID = $(this).attr("id");
        if(formID.indexOf("auto")>-1){
            alert("*" + $(this).val() +"*")  //<-- this returns **
            alert("**"+ $(this).attr("value") + "**")  //<-- this returns the old value
            var origVal= $(this).attr("value")
            alert(origVal)  <-- this alerts the original value
            if($(this).val()==""){
                $(this).val(origVal)  //<-- this does nothing :(
            }
        }
    });
});

尽管我在测试时刚刚意识到,如果用户输入任何旧垃圾并在字段外制表,它就会拾取垃圾值,但我不希望这样,所以它无论如何都不是一个真正的解决方案:(

forceSelection="true"强制您从下拉列表中提供的选项中选择值。不能在字段中输入自定义值。删除forceSelection="true",您的jQuery工作正常,但它接受任何值(不必要的数据)。

这是我的代码

<richui:autoComplete class="required" name="name[${idx}]" id="uniqueId[${idx}]"
     action="${createLinkTo('dir': 'oauthCallBack/test')}" onItemSelect="updateHiddenInput(id, this)" 
     value="${vr}" maxResultsDisplayed="20" minQueryLength="3"/>

<script>
    $(function () {
        $(document).on('change', 'input', function () {
            var formID = $(this).attr("id");
            if (formID.indexOf("auto")) {
                var origVal = $(this).attr("value");
                if (!$(this).val()) {
                    $(this).val(origVal);
                }
            }
        });
    });
</script>