我在我的 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>
关于javascript - 重置 g :autocomplete values when blanked out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20270914/