有条件必填字段和禁用输入
我的表单有一堆地址字段(街道、城市、国家、省、邮政编码),需要一遍又一遍地填写。为了让用户更容易,我添加了一个“复制来源”选择器,他们可以在其中选择以前使用过的地址。当他们这样做时,表单字段会自动填充,然后被禁用。这样他们就可以清楚地看到他们要提交的内容。
问题是,如果禁用这些字段,W3 会说 输入将不会被提交。那很好,因为我可以从选择器中获取所需的所有数据,只是现在表单验证失败。
我想我有两个选择:
- 在提交表单之前重新启用表单字段(但这有点黑客行为)
- 将所有地址字段设为可选。但是然后我需要添加一堆
clean_
方法,以确保当您不使用预设选择器时它们实际上被填充。另外,我在整个网站上都使用地址表单,并且在某些地方确实需要它们,因此我不想将这些字段设为可选。不过,我想我可以复制粘贴地址表单并为此页面制作一个特例。
想法?我应该采取什么方法?
My form has a bunch of address fields (street, city, country, province, postal code) that need to be filled out over and over again. To make it easier for the user, I've added a "copy from" selector where they can choose an address they've previously used. When they do this, the form fields are filled in automatically and then disabled. This way they can clearly see what they're about to submit.
The problem is, if the fields are disabled, W3 says the inputs won't be submitted. That would be fine, because I can get all the data I need from the selector, except that now the form fails validation.
I figure I have two options:
- Re-enable the form fields just before submitting the form (but this is a bit of hack)
- Make all the address fields optional. But then I need to add a bunch of
clean_
methods to make sure they actually are filled in when you don't use the preset selector. Also, I'm using the address form all over the site, and in some places they truly are required, so I'd prefer not to make the fields optional. Although, I guess I could just copy-paste the address form and make a special case for this page.
Thoughts? What approach should I take?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几点想法。他们可能会给你一些想法。
A couple of thoughts. They might give you some ideas.
我一直面临类似的问题,其中需要字段,但它们的值是从另一种表单中获取的。我决定执行以下操作:
这是我的解决方案:
带有表单的 HTML 文件
JS 文件
I've been facing a similar problem, where fields are required but their values are fetched from another form as such. What I decided to do is the following:
Here's my solution:
HTML FILE WITH FORM
JS FILE