javascript自定义多级Select

因项目需要做了一个select 的多级分类:

以下代码用到了jquery库

 

var test = {"1":{"LED":{"5":"3000K","6":{"4000K":{"7":{"16mm":{"9":"成功!"}},"8":{"18mm":{"10":"tset","11":"test2"}}}}}},"2":"电子牛","3":"透镜","4":"五金件"} //返回一个对象的键值对,attr=>value function objPropName(obj){ var result=''; for(var p in obj){ if(jQuery.isPlainObject(obj[p])) for(var pp in obj[p]) result = result+',"'+p+'":"'+pp+'"'; else result = result+',"'+p+'":"'+obj[p]+'"'; } return '{'+result.substring(1)+'}'; } function categoryChange(obj,str_index){ var category1_choose_value = obj.value; var category1_choose_name = obj.options[obj.selectedIndex].text; str_index_cur = str_index+"['"+category1_choose_value+"']['"+category1_choose_name+"']"; var choosed_obj = eval('(category_json'+str_index_cur+')'); $(obj).next().remove(); if(jQuery.isPlainObject(choosed_obj)){ category2data_str = objPropName(choosed_obj); category2data = eval('('+category2data_str+')'); str_index_cur = str_index_cur.replace(g,""); str_index_cur = str_index_cur.replace(//'/g,"//'"); alert(str_index_cur); var str = ''; $(obj).after(str); }else{ alert(category1_choose_value); } }

你可能感兴趣的:(Javascript)