州和国家选择框 - 最好的方法是什么?
这可能看起来微不足道,但我正在构建的个人资料表单页面上设置一个国家和州选择框,这样当您选择美国或加拿大时,州框将显示所选国家/地区的州,否则它会显示“无适用”。 我的国家和州位于数据库中,我正在从中填充我的选择 - 我想要一种简单而正确的方法来执行此操作 - 我注意到由于某种原因,所有浏览器都不支持禁用选择选项:( - 或者是网上有任何不错的免费片段我可以使用[也许我懒得在这里编码]
不过我在这里使用 JQuery 进行 JavaScript 编辑
感谢您的回复
- 级联下拉列表似乎可以满足我的需要,但是。我正在寻找基于 php 的解决方案。
说一句,因为我不想将其留给用户,并以包括美国/加拿大以外的国家/地区的条目结束。
顺便 Ajax 的想法是我的想法,但问题是我正在构建的申请表有一个部分,您可以在其中添加联系人地址。它的构建使得您可以向同一个联系人添加多个地址。只是使用 javascript 函数复制地址输入,所以基本上当您提交表单时,您会得到如下数据:
_POST['address[]'], _POST['city[]'], _POST['state[]'],_POST[ 'country[]']
问题是在创建时将此操作绑定到州和国家的每个实例。
this may seem trivial but I'm setting up, on a profile form page I'm building, a countries and states select box such that when you select the US or Canada then the states box would display states of the selected countries else it would display a None Applicable instead. My countries and states are in a database and I'm populating m y selects from that - I would like a simple yet proper way to do this - I noticed that for some reason disabling select options is not supported in all browsers :( - or is there any nice free snippet online I could use [maybe I'm feeling too lazy to code this here]
I'm using JQuery for the javascripting here though.
Edited
Thanks for the replies - the cascading drop down seems to do what I need but I'm looking for a php based solution.
How have mainstream websites accomplished this btw. Because I don't want to leave it to the user and end with entries including American/ Canadian states with countries that are not the US/Canada.
The Ajax idea is what I had in mind but the thing is that the application form I'm building has a section where you can add contact addresses. Its been built such that you can add multiple addresses to the same contact. Theres an add button which just duplicates the address inputs using a javascript function so basically when you submit the form you have data like :
_POST['address[]'], _POST['city[]'], _POST['state[]'],_POST['country[]']
The thing is binding this action to each instance of state and countries when created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果国家选择器中既没有选择美国也没有选择加拿大,我想我什至不会显示国家选择器。 这种方法有两个优点,来自所有其他国家/地区的用户不会被无意义的内容所困扰,您不必处理不需要的输入。
现在,我将通过 AJAX 保存国家/地区选择器的值,然后根据需要发送/激活带有响应的附加选择器 div。
编辑后:那么您的灵活性如何? 如果您受困于某些现有代码,那么消除不必要的状态条目的 cron 作业可能是另一种方法。
我的意思是您也可以稍后在检索时处理状态,但我假设还有预先存在的代码来创建地址,并且该代码不会检查状态是否有意义......
I think I would not even show the state selector, if neither US nor Canada has been selected from the country selector. This approach has two advantages, users from all other countries are not bothered by meaningless content, you don't have to deal with unwanted input.
Now I would save the value of the country selector via AJAX and then send/activate the additional selector div with the response, if needed.
After your edit: How flexible are you then? If you are stuck with some existing code then a cron job which eliminates unnecessary state entries might be another way.
I mean you could also handle the state later on retrieval but I assume there is also preexisting code which creates addresses and that code doesn't check if the state makes sense...
将 onchanged 处理程序附加到 c]Country 下拉列表。 在处理程序中,使用 Ajax 检索所选国家/地区的州/省列表,使用该列表填充州/省下拉列表并启用州/省下拉列表。
在静态 HTML 中,禁用“州/省”下拉列表。
Attach an onchanged handler to the c]Country dropdown. In the handler, use Ajax to retrieve a State/Province list for the selected country, the populate the State/Province dropdown with that list and enable the State/province dropdown.
In the static HTML, disable the State/Province dropdown.
查看 CascadingDropdown 扩展程序是否可以实现您想要的功能。
See if the CascadingDropdown Extender can do what you are looking for.
我在 ASP.NET 中执行此操作的方法是对国家/地区下拉列表使用缓存的用户控件。 此下拉列表附加了一个RequiredFieldValidator,其初始值设置为“选择一个”。 国家下拉列表可以包含在同一用户控件中,并且可以根据基于国家下拉列表选择的 AJAX 请求进行填充。 (正如@tpdi 在他的回答中提到的)
The way I do it in ASP.NET is to use a cached Usercontrol for the Countries dropdownlist. This dropdownlist has a RequiredFieldValidator attached to it with initial value set to "Select one". The States dropdownlist could be included in the same usercontrol and could be populated based on an AJAX request based on selection of the Countries dropdownlist. (as @tpdi mentions in his answer)