免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1906 | 回复: 6
打印 上一主题 下一主题

HTML网页SELECT元素问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-26 11:15 |只看该作者 |倒序浏览

  1. <select name="season" >;
  2.   <option value="1">;春</option>;
  3.   <option value="2">;夏</option>;
  4.   <option value="3">;秋</option>;
  5.   <option value="4">;冬</option>;
  6. </select>;
复制代码


在网页打开时,显示出来的总是第一项(春).
value(1,2,3,4)的值是从数据库中取出来的,怎么样才能让它自动显示对应的项,比如我数据库中取得的值是3,要让它显示“秋”,谢谢。

论坛徽章:
0
2 [报告]
发表于 2005-01-26 11:24 |只看该作者

HTML网页SELECT元素问题

让value与数据库中取得的值相等是输出selected属性
像这样<option value="3" selected="selected">;秋</option>;

论坛徽章:
0
3 [报告]
发表于 2005-01-26 11:32 |只看该作者

HTML网页SELECT元素问题

3x

论坛徽章:
0
4 [报告]
发表于 2005-01-26 20:50 |只看该作者

HTML网页SELECT元素问题


  1. /*
  2. * Created on 2004-12-12
  3. */
  4. package com.jilaninfo.commons.html;

  5. /**
  6. * @author elgs
  7. */
  8. public class Select implements Options
  9. {

  10.     public Select(Object[][] data)
  11.     {
  12.         this.init(data);
  13.     }

  14.     private Object[][] data          = null;
  15.     private int        selectedIndex = -1;

  16.     public void init(Object[][] data)
  17.     {
  18.         checkData(data);
  19.         init();
  20.         this.data = data;
  21.     }

  22.     private void init()
  23.     {
  24.         this.data = null;
  25.         this.selectedIndex = -1;
  26.     }

  27.     private void checkData(Object[][] data)
  28.     {
  29.         int rows = data.length;
  30.         if (rows < 2)
  31.         {
  32.             throw new IllegalArgumentException("Too few columns specified.");
  33.         }
  34.         for (int i = 0; i < rows; ++i)
  35.         {
  36.             Object o = data[i][0];
  37.             if (o == null)
  38.             {
  39.                 throw new IllegalArgumentException("Null key found.");
  40.             }
  41.             String s = o.toString();
  42.             for (int j = i + 1; j < rows; ++j)
  43.             {
  44.                 if (s.equalsIgnoreCase(data[j][0].toString()))
  45.                 {
  46.                     throw new IllegalArgumentException(s + " is not unique.");
  47.                 }
  48.             }
  49.         }
  50.     }

  51.     /*
  52.      * (non-Javadoc)
  53.      *
  54.      * @see com.jilaninfo.commons.html.Options#getOptions(java.lang.Object[][])
  55.      */
  56.     public String getOptions()
  57.     {
  58.         int rows = data.length;
  59.         StringBuffer sb = new StringBuffer();
  60.         for (int i = 0; i < rows; ++i)
  61.         {
  62.             sb.append("<OPTION VALUE=\"");
  63.             sb.append(data[i][0]);
  64.             sb.append(i == selectedIndex ? "\" SELECTED>;" : "\">;");
  65.             sb.append(data[i][1]);
  66.         }
  67.         return sb.toString();
  68.     }

  69.     /*
  70.      * (non-Javadoc)
  71.      *
  72.      * @see com.jilaninfo.commons.html.Options#setSelectedIndex(int)
  73.      */
  74.     public void setSelectedIndex(int index)
  75.     {
  76.         this.selectedIndex = index;
  77.     }

  78.     /*
  79.      * (non-Javadoc)
  80.      *
  81.      * @see com.jilaninfo.commons.html.Options#getSelectedIndex()
  82.      */
  83.     public int getSelectedIndex()
  84.     {
  85.         return selectedIndex;
  86.     }

  87.     /*
  88.      * (non-Javadoc)
  89.      *
  90.      * @see com.jilaninfo.commons.html.Options#setSelectedKey(java.lang.String)
  91.      */
  92.     public void setSelectedKey(String key)
  93.     {
  94.         int rows = data.length;
  95.         for (int i = 0; i < rows; ++i)
  96.         {
  97.             if (key.equalsIgnoreCase(data[i][0].toString()))
  98.             {
  99.                 this.selectedIndex = i;
  100.                 break;
  101.             }
  102.         }
  103.     }

  104.     /*
  105.      * (non-Javadoc)
  106.      *
  107.      * @see com.jilaninfo.commons.html.Options#getSelectedKey()
  108.      */
  109.     public String getSelectedKey()
  110.     {
  111.         return data[this.selectedIndex][0].toString();
  112.     }

  113. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2005-01-27 16:40 |只看该作者

HTML网页SELECT元素问题

感谢楼上的兄弟,HTML元素到了页面上确实不好处理,都自动转化。

论坛徽章:
0
6 [报告]
发表于 2005-01-27 23:17 |只看该作者

HTML网页SELECT元素问题

恩,
最好用
selected = "selected"
不要只用
selected

论坛徽章:
0
7 [报告]
发表于 2005-01-28 00:18 |只看该作者

HTML网页SELECT元素问题

原帖由 "jhsea3do" 发表:
恩,
最好用
selected = "selected"
不要只用
selected
同意,要尽量符合标准啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP