使用javascript选择下拉菜单选项 [英] Select dropdown menu option with javascript

查看:91
本文介绍了使用javascript选择下拉菜单选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单,我无法弄清楚如何使javascript功能选择一个下拉菜单选项。我测试了变量的输出,它们都是正确的,但是当单击时仍然不会选择该选项。这是功能和下拉菜单。

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 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>


推荐答案

更改以下行:

document.getElementById(stateSelect)。selected = true;

to:

document.getElementById(stateSelect)。selectedIndex = i;

这篇关于使用javascript选择下拉菜单选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆