有没有办法强制用户在提交表单之前选择自动完成值?

发布于 2024-09-04 04:16:33 字数 331 浏览 3 评论 0原文

我正在使用 jqueryui 自动完成功能来允许用户选择他们的位置。我需要确保他们在提交表单之前从自动完成中选择一个位置。我不希望他们在选择值之前能够提交表单。

我的解决方案是在他们提交后确保找到的值位于数据库中。另一个解决方案是,当他们单击“提交”或离开位置框并转到另一个字段通过 ajax 提交条目时,以确保它是有效的(如果不显示错误)并且不让他们提交表单。

我想知道是否可以做一些更简单的事情,或者是否有更优雅的事情。我意识到他们必须能够在框中输入一些内容才能使自动完成功能正常工作,因此我可以强制他们从自动完成功能中选择一个值,因为它不是选择框,所以看来我的上述解决方案是最好的办法。

想法?

I am using the jqueryui autocomplete feature to allow the user to select their location. I need to make sure they select a location from the autocomplete before they can submit the form. I don't want them to be able to submit the form before they select a value.

My solution is after they submit make sure that the value found is in the database. The other solution would be that when they click submit or after they leave the location box and go to another field to submit the entry via ajax to ensure it is valid if not show an error and do not let them submit the form.

I was wondering if there was something easier I could do, or if there is something more elegant. I realize they have to be able to type something into the box to get the autocomplete to work, so I could force them to select a value from the autocomplete because it is not a select box, so it seems that my above solutions would be the best way to go.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨夜星沙 2024-09-11 04:16:34

在任何情况下,您都需要保留服务器端检查以避免安全和其他错误。但是,您可以添加客户端验证以更加用户友好。

本质上,如果您禁用提交按钮并让表单的 onsubmit“返回 false”,同时禁用提交按钮,您可以帮助确保用户在您这么说之前不会提交 - 通过在表单的值是时启用提交按钮有效的。

要在客户端验证表单,由于您已经在使用 jquery,您可能需要查看 验证 jquery 插件

You'll need to retain server-side checking to avoid security and other bugs in any case. However, you can add client side verification to be more user-friendly.

In essence, if you disable the submit button and have the form's onsubmit "return false" while the submit button is disabled, you can help ensure the user doesn't submit until you say so - by enabling the submit button when the form's value's are valid.

To validate a form on the client side, since you're using jquery already, you may want to look at the validate plugin for jquery.

瀞厅☆埖开 2024-09-11 04:16:34

如果用户单击了自动完成条目,我会设置一个标志,如果文本框被修改,我会清除该标志。

I would set a flag if the user has clicked an autocomplete entry and clear the flag if the textbox is modified.

束缚m 2024-09-11 04:16:34

我建议,根本不使用提交按钮,而是使用一个简单的按钮,您可以在其中使用 JavaScript 附加点击事件。这样,当用户单击它时,您可以验证您需要的任何内容并自行提交表单。

我不认为发出 AJAX 请求来验证某个字段的输入有什么意义,除非您确实必须将资源使用降至最低限度。我这样说是因为,如果您要执行 AJAX 请求进行验证,您可以简单地发送整个表单并在后端进行验证,除非表单非常庞大。

What I would suggest, is not using a submit button at all, but a simple button where you attach a click event with your javascript. In this way, when the user clicks it, you can validate whatever you need and submit the form yourself.

I do not see the point of going as far as making an AJAX request to validate the input of one of your fields, unless you really must take resource use to a minimal point. I say this because, if you are going to do AJAX requests to validate, you may as simply send the whole form and validate in the backend already, unless is a really huge form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文