开发者

IE6 & IE7 How to obtain the selected option object form the select box

I have a code like this:

var a = selectBox.options[selectBox.selectedIndex];

If I execute:

alert(a);

Using FF, Chrome, Opera, etc... I get:

[object HTMLOptionElement]

Which is exactly what I want. If I use IE6 or IE7, I get:

[object]

With no expected Option properties at all.

This means that there is no TextNode as a childNode and no value property. How can I solve that problem? Also if I try to use that node to add to another select box IE throws an exception:

SCRIPT87: Invalid argument.

I was requested to make a program that is compatible with IE6 but I can't find a way to make it work. I need to move Option objects around and IE7 is not allowing me.

Edit by request:

Context HTML

                <select id="identification">
                    <option value="1"开发者_运维知识库>1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>

                    <select id="toBeFilled" multiple>
                </select>

Important note:

What I'm making NEVER knows the list of names and ids of the document. It only knows the nodes which it will use to work in the document.

When I state Node I mean, for instance, what document.getElementById() returns.


var a = selectBox.options[selectBox.selectedIndex]; will definitely give you an Option object (or HTMLOptionElement) in all browsers, IE 7, 6, 5, 4 and possibly 3 included (although IE may not give you a particularly helpful value when you call toString(), as you've observed). This will have text and value properties that reflect the text and value of the option. If this isn't working for you then there's a problem elsewhere in your code.

As for transferring options between select boxes, I'm not so sure on this, but if in doubt you could just create a new Option and add that to your new select box:

var newOption = new Option(a.text, a.value);


var a = selectBox.options[selectBox.selectedIndex].value; will help you

(Alerts can only print Strings and Numbers, but no objects. You can use "console.log( a )" in Firebug and IE8+ with developer Toolbar

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜