[英]jQuery autocomplete how to integrate this code with our function to validate?
I'm using jQuery autocomplete in a zend project. 我在zend项目中使用jQuery自动完成功能。 The input displays a list of categories from our database.
输入显示数据库中的类别列表。 I just realized the form will still submit no matter what the user enters.
我只是意识到无论用户输入什么,表单仍将提交。 I looked for a way to validate the field so they could only select an option from the database / autocomplete.
我正在寻找一种验证字段的方法,以便他们只能从数据库/自动完成中选择一个选项。 I found this code which works
我发现此代码有效
var src = ['hi', 'bye', 'foo', 'bar'];
$("#auto").autocomplete({
source: function(request, response) {
var results = $.ui.autocomplete.filter(src, request.term);
if (!results.length) {
this.element.val('');
}
response(results);
}
});
My JS looks like this, i'm not sure how to use the code above to validate our variables coming from the autocomplete. 我的JS看起来像这样,我不确定如何使用上面的代码来验证来自自动完成功能的变量。 Any help is appreciated thanks
任何帮助表示赞赏,谢谢
$(function() {
var url = "http://domain.com/account/ajaxautocomplete?format=json";
$( "#autotest" ).autocomplete({
source: url,
minLength: 2
});
});
You are probably using the wrong approach. 您可能使用了错误的方法。 You could use instead of the auto-completion a select-tag.
您可以使用选择标签代替自动补全标签。 Then the user can only make a valid selection and he knows what he can select.
然后,用户只能进行有效选择,他知道自己可以选择什么。 But if you want to stick with the auto-completion you need to check the input independent from the auto-completion-tool (it's not intended for validation).
但是,如果您要坚持使用自动完成功能,则需要独立于自动完成工具来检查输入(它不用于验证)。 You could check the data before it's send or with an keyup-event-listener.
您可以在发送数据之前或使用keyup-event-listener检查数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.