我在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 :(
}
}
});
});
虽然我在测试时已经意识到,如果用户输入任何旧的垃圾并从字段中删除它然后获取垃圾值,但我不希望这样,所以它不是真正的解决方案:(
答案 0 :(得分:0)
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>