<选择>其值取决于另一个

发布于 2024-08-01 20:14:59 字数 371 浏览 8 评论 0原文

我可以想到一种方法来做到这一点,但我不知道这是否是唯一的方法。

在我网站的页面上,我有两个选择框,一个“父”选择框和一个“子”选择框。 “子”选择框中的值取决于“父”选择框中选择的内容。 在 JavaScript 中,通过 AJAX 调用很容易解决这个问题,但是我尝试以这样的方式构建我的网站:当 JavaScript 关闭时,它们会正常降级。

对于非 js 用户,我能想到的处理这种情况的唯一方法是使用一个表单按钮,该按钮将重新加载 PHP 页面并将变量附加到 URL,然后 PHP 将检测到这一点并将值发送到“孩子的选择。 然而,这有点可用性问题,在我看来,这是一种丑陋的解决方法。

我还有其他方法可以做到这一点吗? 这些值不一定需要位于选择框中。

I can think of one way to do this, however I don't know if its the only way.

On a page on my website, I have a two select boxes, a 'parent' select box and a 'child' select box. The values that are inside the 'child' select box depend on what is selected in the 'parent' select box. This would be easy to solve in JavaScript, via AJAX calls, however I try to build my websites in such a way that they degrade gracefully when JavaScript is turned off.

The only way I can think of approaching this scenario for a non-js user is to have a form button that will reload the PHP page and append a variable to the URL, and then the PHP will detect this and send the values to the 'child' select. However this is a bit of a usability problem and IMO, an ugly way to approach this.

Are there any other ways I could go about doing this? The values don't necessarily need to be in select boxes.

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

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

发布评论

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

评论(4

通知家属抬走 2024-08-08 20:14:59

使用 OPTGROUP可以帮助 JavaScript 过滤和禁用 JavaScript 时的可用性(假设没有大量选项)。

对于第二个 SELECT:

<select>
  <optgroup name="colors" label="Colors">
    <option value="red">Red</option>
    <option value="blue">Blue</option>
  </optgroup>
  <optgroup name="sizes" label="Sizes">
    <option value="small">Small</option>
    <option value="medium">Medium</option>
    <option value="large">Large</option>
  </optgroup>
</select>

请记住,即使启用了 JavaScript,服务器端代码也需要验证选项。

Using OPTGROUPs can help both with the JavaScript filtering and with usability when JavaScript is disabled (assuming there aren't a huge number of options).

For the second SELECT:

<select>
  <optgroup name="colors" label="Colors">
    <option value="red">Red</option>
    <option value="blue">Blue</option>
  </optgroup>
  <optgroup name="sizes" label="Sizes">
    <option value="small">Small</option>
    <option value="medium">Medium</option>
    <option value="large">Large</option>
  </optgroup>
</select>

Just remember, server side code needs to validate options even if JavaScript is enabled.

套路撩心 2024-08-08 20:14:59

禁用 Javascript 后,您别无选择,只能等待用户提交回服务器。

如果父选择中的值足够小,您可能可以为每个值选择一个,从而无需更新内容。

老实说,我不会打扰:统计数据显示 1% 或更少的用户禁用了 Javascript。 更多的人使用 Safari 和 Opera,很多人甚至懒得在这些浏览器上测试他们的网站。

要么您必须选择一组不同的 UI 元素,以便更优雅地降级。

最后,值得指出的是,如果网站做得足够好,您可以在 300 毫秒内完成页面刷新,此时返回服务器的成本(就用户体验而言)并不多。

With Javascript disabled you have no choice but to wait for the user to submit back to the server.

If the values in your parent select are sufficiently small, you might be able to get away with having one select for each value, which will bypass the need to update the contents.

Honestly though, I wouldn't bother: stats show 1% or less of users have Javascript disabled. More people use Safari and Opera and a lot of people don't even bother to test their sites on those browsers.

Either that or you have to choose a different set of UI elements that will degrade more gracefully.

Lastly, it's worth pointing out that if a Website is done sufficiently well, you can get page refreshes done in under 300ms at which point there isn't much of a cost (in terms of user experience) of going back to the server.

人间☆小暴躁 2024-08-08 20:14:59

除非确实有数千种可能的选择,否则您不需要 AJAX。 您可以简单地将所有可能的值加载到第二个选择中(使用选项组以提高可读性),并在选择父项后使用 JS 过滤它们。 您还需要使用 JS 禁用第二个框,以便用户始终首先选择父框。

Unless there are literally thousands of possible options you shouldn't need AJAX. You can simply load all possible values into the second select (with option groups for readability), and use JS to filter them once a parent is selected. You'll also want to disable the second box with JS, so the user always selects a parent first.

痴意少年 2024-08-08 20:14:59

不幸的是,如果您想将表单全部保留在一页上,则没有真正的好方法可以在不编写脚本的情况下解决此问题 - 您对按钮的想法可能是您可以获得的最佳方法。

另一种选择是将表单转变为多步骤过程,在其中选择一个页面上的父项目,然后移动到具有子选择的过程中的下一页 - 本质上是向导式的方法。

Unfortunately, there's not really a good way to approach this without scripting if you want to keep the form all on one page - the idea you had with the button is probably the best you could get.

The alternate option would be to turn the form into a multi-step process, where they select the parent item on one page, and then are moved on to the next page in the process which has the child select - essentially, a wizard-style approach.

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