admin管理员组

文章数量:1391991

Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.

Because this is IE Windows Mobile 5, most solutions involving getElementID do not function, so I have had to resort to more plain vanilla HTML /Java script, the example below works fine in IE 6 and FF , but fails with "Object doesn't support this property or method" in Windows Mobile 5

function insertBarcodes()
{
val = document.form1.barcode.value ;
i = document.form1.blist.length;
myNewOption = new Option(val , val ); 
document.form1.blist.options[document.form1.blist.length] =myNewOption ; 
 }
 updateCount();

}

Any ideas?

Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.

Because this is IE Windows Mobile 5, most solutions involving getElementID do not function, so I have had to resort to more plain vanilla HTML /Java script, the example below works fine in IE 6 and FF , but fails with "Object doesn't support this property or method" in Windows Mobile 5

function insertBarcodes()
{
val = document.form1.barcode.value ;
i = document.form1.blist.length;
myNewOption = new Option(val , val ); 
document.form1.blist.options[document.form1.blist.length] =myNewOption ; 
 }
 updateCount();

}

Any ideas?

Share Improve this question edited Feb 8, 2010 at 22:34 Matt Lacey 65.6k12 gold badges93 silver badges145 bronze badges asked May 22, 2009 at 16:12 tonyBtonyB
Add a ment  | 

3 Answers 3

Reset to default 2

There are 4 ways (that I know of) to set the options... (hopefully one of them works for you (let us know which))

//option 1
var newOpt = document.createElement('option');
newOpt.innerText = 'Hello';
mySelectObject.appendChild(newOpt);

//option 2
mySelectObject.innerHTML = '<option>Hello</option>';
//KNOWN TO FAIL IN IE6,7,8 (see url below)

//option 3
mySelectObject.outerHTML = '<select><option>Hello</option></select>'; //IE Only

//option 4
var newOpt = new Option('Hello','Hello');
mySelectObject.options[index] = newOpt;

IE bug with setting the .innerHTML

Found the answer here:

First I looked at the official reference source here: http://msdn.microsoft./en-us/library/bb159677.aspx

I noted that there is an add method for the selectObj, so I tried it and it worked..

here's the working code,

function AddSelectOption(selectObj, text, value, isSelected){
  if(selectObj != null && selectObj.options != null){
    var newOpt = new Option('Hello','Hello'); //create the option object
    selectObj.add(newOpt); //it's the .add(option) method
  }
}

Thanks to all

From steven harman's blog:

function AddSelectOption(selectObj, text, value, isSelected) 
{
    if (selectObj != null && selectObj.options != null)
    {
        selectObj.options[selectObj.options.length] = 
            new Option(text, value, false, isSelected);
    }
}

So your code would bee;

function insertBarcodes()
{
    val = document.form1.barcode.value ;
    AddSelectOption( document.form1.blist, val, val, false );
}

The site states that the author ran into the exact issue you mentioned. The author admits that he doesn't know WHY a four-parameter Option object works, only that it does.

本文标签: javascriptHow to Add options to ltSELECTgtin IE Windows Mobile 5Stack Overflow