尝试访问特定选项值以生成弹出窗口

时间:2011-01-11 04:48:29

标签: javascript arrays drop-down-menu popup

我正在尝试使用click事件根据所选的特定值生成弹出窗口。我在使用if语句时遇到问题并尝试访问每个特定的选项值。你们有人能给我一些提示吗?

        <select id="offices">
            <option value="Choose an Office">Choose an Office</option>
            <option value="Residential Education (ResEd)" >Residential Education (ResEd)</option>
            <option value="Dean of Students">Dean of Students</option>
            <option value="Office of Student Affairs">Office of Student Affairs</option>
            <option value="Vice-Provost of Student Affairs">Vice-Provost of Student Affairs</option>
        </select>
</div>

function display(){

        var officearray = [{
            Office: "Residential Education (ResEd)",
            ID: "725-2800",
            Description: "The Office of Residential Education is responsible for developing the policies, programs, and staffing which support the intellectual, educational, and community-building activities in student residences.  Second Floor. "
        }, {
            Office: "Dean of Students",
            ID: "723-7833",
            Description: "The Dean of Students office is composed of 13 individual administrative units that are concerned with the general welfare of both undergraduate and graduate students, in and out of the classroom.  Second floor."
        }, {
            Office: "Office of Student Activities (OSA)",
            ID: "723-2733",
            Description: "Services for student organizations, student-initiated major events and programs, and fraternities and sororities.  Second floor."
        }, {
            Office: "Vice-Provost of Student Affairs",
            ID: "725-0911",
            Description: "The Vice Provost for Student Affairs is responsible to the Provost for providing services and programs to undergraduate and graduate students in support of the academic mission of the University.  Second floor."
        }]

        for(var i = 0; i < officearray.length; i++) {
            var o = document.getElementById("offices")
            var oString = o.options[o.selectedIndex].value;

            newwindow2 = window.open('', 'name', 'height=200, width=150')
            var tmp = newwindow2.document
            if (oString == officearray[i].Office) {
                tmp.writeln(officearray[i].Description)
            }

        }
    }
    document.getElementsByTagName('option').addEventListener("click",display,false)

2 个答案:

答案 0 :(得分:0)

假设我理解你的问题,请尝试用以下代码替换最后一行:

var options = document.getElementsByTagName('option');
for(var i = 1; i < options.length; i++) { options[i].addEventListener("click", display, false) }

您正在尝试对元素数组使用AddEventListener,这将无效。

答案 1 :(得分:0)

感谢您的回复。相反,我只是将onlick事件更改为onchange并删除了addEventListener时段。然后,我使用符合W3C的事件处理程序来处理更改事件。

document.getElementById("offices").onchange=display;