Create optgroup element into select list with DOM
Posted by essoduke - 2007 年 12 月 20 日 00:22:09 - 326 User Views今天在研究使用 Ajax 透過 jQuery 動態建立 Optgroup 清單。
混沌兩個月,終於寫出稍微有用的文章了…
- var obj = document.getElementById("list");
- $.getJSON("ajax.asp", {id: idx}, function(json){
- // if json data created...
- if(json.length > 0) {
- // get json data of optgroup
- for(var i=0; i<json .length; i++) {
- var oGroup = document.createElement("optgroup");
- // setting label attribute
- oGroup.label = json[i].title;
- // insert optgroup element to select list
- obj.appendChild(oGroup);
- // get json data of option
- for(var j=0; j<json[i].product.length; j++) {
- oOption = document.createElement("option");
- //setting title attribute
- oOption.label = "Option1";
- //setting value attribute
- oOption.value = "Value of Option1";
- //insert option element to select list
- obj.appendChild(oOption);
- }
- }
- }
- });
JSON Data Example
- [
- {
- title:'optgroup1',
- product:[
- {
- id:1,
- title:'Demo1',
- price:100
- },
- {
- id:2,
- title:'Demo2',
- price:200
- }
- ]
- },
- {
- title:'optgroup2',
- product:{
- id:3,
- title:'Demo3',
- price:300
- }
- }
- ]
Reference
Technorati Tags: DOM, javascript, jQuery
回首過往今日
相關文章
No Comments yet »
發表迴響
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.



