dede5.6联动模型一级二级自适应~
解决自定义内容模型中,联动类型只有一级分类时也会出现两个下来菜单的问题,目的在于改善用户体验.
因为普通用户使用时会看到两个下来菜单,即使只有一个菜单有效时,还是有很多用户认为第二个下拉菜单里也需要选择,但是什么也点不出来就一直在那里等待.
效果:
同时提供一个完整文件下载. 请覆盖 /images/ 目录下的同名文件.
文件名 enums.js
修改部分已经做了标注.
- <!–
- // jim.ma QQ:164186 jim.mail@163.com www.itfly.com
- // 2010-12-05 DedeCMS5.6 一级二级菜单自动适应
- //选择地区的二级分类(非通用调用)
- function selNext(oj, v)
- {
- var newobj = oj.options;
- var selv = parseInt(v);
- var maxv = parseInt(v) + 500;
- while(newobj.length > 0) {
- oj.remove(0);
- }
- clear(oj);
- if(selv==0)
- {
- aOption = document.createElement(‘OPTION’);
- aOption.text = ‘具体地区’;
- aOption.value = ‘0’;
- oj.options.add(aOption);
- return;
- }
- else
- {
- aOption = document.createElement(‘OPTION’);
- aOption.text = ‘具体地区’;
- aOption.value = ‘0’;
- oj.options.add(aOption);
- }
- var str = ”;
- for(i=selv+1; i < maxv; i++)
- {
- if(!em_nativeplaces[i]) continue;
- aOption = document.createElement(‘OPTION’);
- aOption.text = em_nativeplaces[i];
- aOption.value = i;
- oj.options.add(aOption);
- }
- }
- //子类改变事件
- function ChangeSon()
- {
- var emname = this.name.replace(‘_son’, ”);
- var topSelObj = document.getElementById(emname+’_top’);
- if(this.options[this.selectedIndex].value==0) {
- document.getElementById(‘hidden_’+emname).value = topSelObj.options[topSelObj.selectedIndex].value;
- }
- else {
- document.getElementById(‘hidden_’+emname).value = this.options[this.selectedIndex].value;
- }
- }
- //顶级类改变事件
- function selNextSon()
- {
- var emname = this.name.replace(‘_top’, ”);
- if( document.getElementById(emname+’_son’) )
- {
- var oj = document.getElementById(emname + ‘_son’);
- }
- else
- {
- var oj = document.createElement(‘select’);
- oj.name = emname + ‘_son’;
- oj.id = emname + ‘_son’;
- oj.onchange = ChangeSon;
- }
- var v = this.options[this.selectedIndex].value;
- document.getElementById(‘hidden_’+emname).value = v;
- var newobj = oj.options;
- var selarr = eval(’em_’+emname+’s’);
- var selv = parseInt(v);
- var maxv = parseInt(v) + 500;
- while(newobj && newobj.length > 0) oj.remove(0);
- clear(oj);
- if(selv==0)
- {
- aOption = document.createElement(‘OPTION’);
- aOption.text = ‘请选择..’;
- aOption.value = ‘0’;
- oj.options.add(aOption);
- return;
- }
- else
- {
- aOption = document.createElement(‘OPTION’);
- aOption.text = ‘请选择..’;
- aOption.value = ‘0’;
- oj.options.add(aOption);
- }
- var str = ”;
- for(i=selv+1; i < maxv; i++)
- {
- if(!selarr[i]) continue;
- aOption = document.createElement(‘OPTION’);
- aOption.text = selarr[i];
- aOption.value = i;
- oj.options.add(aOption);
- }
- document.getElementById(‘span_’+emname+’_son’).appendChild(oj);
- }
- //————————————-by jim.ma 一级菜单响应
- function selNextSon1()
- {
- var emname = this.name.replace(‘_top’, ”);
- var v = this.options[this.selectedIndex].value;
- document.getElementById(‘hidden_’+emname).value = v;
- }
- //————————————EOF
- //生成任意的两级选择框
- function MakeTopSelect(emname, selvalue)
- {
- var selectFormHtml = ”;
- var aOption = null;
- var selObj = document.createElement(“select”);
- selObj.name = emname + ‘_top’;
- selObj.id = emname + ‘_top’;
- selObj.onchange = selNextSon;
- var selarr = eval(’em_’+emname+’s’);
- var topvalue = 0;
- var sonvalue = 0;
- aOption = document.createElement(‘OPTION’);
- aOption.text = ‘请选择..’;
- aOption.value = 0;
- selObj.options.add(aOption);
- if(selvalue%500 == 0 ) {
- topvalue = selvalue;
- }
- else {
- sonvalue = selvalue;
- topvalue = selvalue – (selvalue%500);
- }
- var arrlen1=0;//————————————-by jim.ma
- for(i=500; i<=selarr.length; i += 500)
- {
- if(!selarr[i]) continue;
- aOption = document.createElement(‘OPTION’);
- if(i == topvalue) {
- aOption = document.createElement(‘OPTION’);
- aOption.text = selarr[i];
- aOption.value = i;
- selObj.options.add(aOption);
- aOption.selected = true;
- }
- else {
- aOption = document.createElement(‘OPTION’);
- aOption.text = selarr[i];
- aOption.value = i;
- selObj.options.add(aOption);
- }
- arrlen1++;//————————————-by jim.ma
- }
- document.getElementById(‘span_’+emname).appendChild(selObj);
- //————————————-by jim.ma
- var arrlen2 = 0 ;
- for(tv in selarr){
- arrlen2++;
- }
- if(arrlen1 == arrlen2){
- selObj.onchange = selNextSon1;
- return ;
- }
- //————————————-EOF
- //如果子类存在值,创建子类
- //if(sonvalue > 0 || topvalue > 0) {
- selObj = document.createElement(“select”);
- selObj.name = emname + ‘_son’;
- selObj.id = emname + ‘_son’;
- selObj.onchange = ChangeSon;
- aOption = document.createElement(‘OPTION’);
- aOption.text = ‘请选择..’;
- aOption.value = 0;
- selObj.options.add(aOption);
- //当大类有值输出子类
- if(topvalue > 0)
- {
- var selv = topvalue;
- var maxv = parseInt(topvalue) + 500;
- for(i=selv+1; i < maxv; i++)
- {
- if(!selarr[i]) continue;
- aOption = document.createElement(‘OPTION’);
- if(i == sonvalue) {
- aOption = document.createElement(‘OPTION’);
- aOption.text = selarr[i];
- aOption.value = i;
- selObj.options.add(aOption);
- aOption.selected = true;
- }
- else {
- aOption = document.createElement(‘OPTION’);
- aOption.text = selarr[i];
- aOption.value = i;
- selObj.options.add(aOption);
- }
- }
- }
- document.getElementById(‘span_’+emname+’_son’).appendChild(selObj);
- }
- //清除旧对象
- function clear(o)
- {
- l=o.length;
- for (i = 0; i< l; i++){
- o.options[1]=null;
- }
- }
- –>
复制代码
by jim.ma 2010年12月6日
EOF—————————————————————