开发者

Trying to access a specific option value to generate a popup window

开发者 https://www.devze.com 2023-02-03 08:17 出处:网络
I am trying to use a click event to generate a popup window based off of the specific value chosen. I am having trouble with the if statement and trying to access each specific option value. Can any o

I am trying to use a click event to generate a popup window based off of the specific value chosen. I am having trouble with the if statement and trying to access each specific option value. Can any of you give me some hints?

        <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.getEleme开发者_开发技巧ntsByTagName('option').addEventListener("click",display,false)


Assuming I understand your question, try replacing the last line with these:

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

You are trying to use AddEventListener on an array of elements, which won't work.


Thanks for the response. Rather I just changed the onlick event to onchange and got rid of the addEventListener period. I then used the W3C compliant event handler to handle the change event.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号