简体   繁体   中英

Firefox vs IE Javascript issue

I see that there are A LOT of related questions on this one, and reading through them I'm guessing this is a getElementById problem since that seems to be the issue in other people's questions, but I'm not quite sure what I did wrong. The following works in FireFox with no messages on the error console and FireBug displaying the correct values:

    function updateSQ() {
        var sQuestion = document.getElementById('<%=sQuestion.ClientID%>');
        sQuestion.value = "";
        var questions = document.getElementsByName('selectQuestion');
        for (question in questions) {
            if (questions[question].value != null)
                            sQuestion.value += questions[question].value + ",";
        }
        alert(sQuestion.value);
    }

This function is called by a dynamically generated html select tag:

<SELECT id="squestion1" name="selectQuestion" onchange="updateSQ();">
<OPTION value="notChosen">--Please Select One--</OPTION> <OPTION value="in">India</OPTION> <OPTION value="de">Germany</OPTION>
<OPTION value="fr">France</OPTION>
<OPTION value="us">United States</OPTION>
<OPTION value="ch">Switzerland</OPTION>
</SELECT>

As you can see, the javascript is trying to update the value of an ASP.NET control: <asp:HiddenField ID="sQuestion" runat="server" value="" />

The idea is that when the select changes, it calls the JS and stores the values from all of the select statements into the ASP.NET control (sort of a clunky work around for an irritating set of requirements and legacy code I was asked to use).

The final alert has the expected values in Firefox, but in IE7 it is an empty string supporting the idea that I'm just not finding the right tag. Any input is greatly appreciated. Thank you.

I think your problem is with getElementsByName

HTML elements created using Javascript DOM are not accessible by document.getElementsByName method in IE.

Try the following google query and see the plethora of bug reports and frustration on this subject ;)

http://www.google.dk/search?sourceid=chrome&ie=UTF-8&q=getElementsByName+ie

getElementsByName in IE not returns Array but returns Object Use

for (var question  = 0; question < questions.length; question ++)

instead of

for (question in questions) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM