自知其无知
不懂,慢慢懂。
posts - 2,comments - 2,trackbacks - 0

转自: http://www.webdeveloper.com/forum/archive/index.php/t-70927.html

可谓超级经典:

whitedog
06-27-2005, 11:20 AM
Hi Folks,
really think that this is a FAQ but cant find
any docs about this....

How can i populate a select with optgroups
is there any standard constructor like
new Option(text,val,..) which can do???

OK, i could produce the complete HTML
-option stuff dynamically onLoad as text and
via document.write() but i need
to change later on, so this is a nice twaek
but wont help me with this....

BTW, is the any actual documentation about
this topic? develeoper.netscape.com seemed
to have closed down, and most other stuff is
pretty outdated...

REGARDS!

whitedog

 

whitedog
06-28-2005, 07:59 AM
Hey Folks, wazzup?

Anyone any ideas?
Nor have this ever seen in some documentation
and let me know where to find??

Hey Guru's, HELP please!!

 

Orc Scorcher
06-28-2005, 08:24 AM
Well, the problem is that theoretically this is dead simple but in reality IE screws up. Here's an example that works fine in better browsers but the options are invisible in IE6: <select id="theSelect"></select>
<script>

void function() {

var select = document.getElementById("theSelect")
/* Try again if the DOM tree isn't ready yet */
if (!select) setTimeout(arguments.callee, 20)

/* Create the optgroup, it's label and two options
* then add it to the select */
var optG = document.createElement("optgroup")
optG.label = "First Optgroup"
optG.appendChild(new Option("Option 1"))
optG.appendChild(new Option("Option 2"))
select.appendChild(optG)

optG = document.createElement("optgroup")
optG.label = "Second Optgroup"
optG.appendChild(new Option("Option 3"))
optG.appendChild(new Option("Option 4"))
select.appendChild(optG)

}()

</script>

 

BigMoosie
06-28-2005, 08:26 AM
<FORM name="myForm">
<SELECT name="mySelect">
</SELECT>
</FORM>
<SCRIPT type="text/javascript">
var subject=document.createElement("OPTGROUP")
subject.setAttribute("label", "hello");
document.forms.myForm.mySelect.appendChild(subject);
</SCRIPT>

 

Fang
06-28-2005, 08:42 AM
x-browser:var optG = document.createElement("optgroup")
optG.label = "First Optgroup"
select.appendChild(optG)
var oOption = document.createElement("option");
var txt = document.createTextNode("Option 1");
oOption.appendChild(txt);
optG.appendChild(oOption);
select.appendChild(optG)

or

var optG = document.createElement("optgroup")
optG.label = "First Optgroup"
select.appendChild(optG)
select.options[select.options.length] = new Option("Option 1");



 

posted on 2008-12-15 22:51 CopyHoo 阅读(680) 评论(0)  编辑  收藏 所属分类: Java Web

只有注册用户登录后才能发表评论。


网站导航: