使用 JavaScript 选择下拉菜单选项

发布于 2024-11-01 18:36:02 字数 725 浏览 1 评论 0原文

我有一个下拉菜单,但我不知道如何让 JavaScript 函数选择下拉菜单选项。我已经测试了变量的输出,它们都是正确的,但单击时仍然不会选择该选项。这是功能和下拉菜单。

功能

function formFill(a, b, c){
        theform.from.value = a;
        theform.to.value = b;
        for(var i = 0;i < document.getElementById("stateSelect").length;i++){
            if(document.getElementById("stateSelect").options[i].value == c ){
                document.getElementById("stateSelect").selected = true;
            }
        }
    }

菜单项

<select id="stateSelect" name="stateSelect">
    <option value="none">(None)</option>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>

I have a dropdown menu and I cannot figure out how to make a javascript function select a drop down menu option. I have tested the output of the variables and they are all correct, but it still will not select the option when clicked. Here is the function and drop down menu.

Function

function formFill(a, b, c){
        theform.from.value = a;
        theform.to.value = b;
        for(var i = 0;i < document.getElementById("stateSelect").length;i++){
            if(document.getElementById("stateSelect").options[i].value == c ){
                document.getElementById("stateSelect").selected = true;
            }
        }
    }

Menu item

<select id="stateSelect" name="stateSelect">
    <option value="none">(None)</option>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>

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

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

发布评论

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

评论(3

傾旎 2024-11-08 18:36:02

将以下行:

document.getElementById("stateSelect").selected = true;

更改为:

document.getElementById("stateSelect").selectedIndex = i;

Change the line that reads:

document.getElementById("stateSelect").selected = true;

to:

document.getElementById("stateSelect").selectedIndex = i;

浅唱ヾ落雨殇 2024-11-08 18:36:02

替代。您可以将 selected 设置为实际选项: select.options[i].selected = true;

...
        var select = document.getElementById("stateSelect");
        for(var i = 0;i < select.options.length;i++){
            if(select.options[i].value == c ){
                select.options[i].selected = true;
            }
        }
...

Alt. you can set selected to the actual option: select.options[i].selected = true;

...
        var select = document.getElementById("stateSelect");
        for(var i = 0;i < select.options.length;i++){
            if(select.options[i].value == c ){
                select.options[i].selected = true;
            }
        }
...
流年已逝 2024-11-08 18:36:02

例如,使用 Stimulus,我们获取用户的时区,然后在下拉列表中自动选择它

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["input"]

  connect() {
    const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

    [...this.inputTarget.options].forEach((option) => {
      if (option.value == timeZone) {
        option.selected = true
      }
    })
  }
}

With Stimulus, for instance we fetch the user's time zone and then we auto-select it in the dropdown

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["input"]

  connect() {
    const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

    [...this.inputTarget.options].forEach((option) => {
      if (option.value == timeZone) {
        option.selected = true
      }
    })
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文