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 tonyBtonyB3 Answers
Reset to default 2There 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
版权声明:本文标题:javascript - How to Add options to <SELECT>, in IE Windows Mobile 5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744719837a2621629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论