html:select的取值问题

bhwhy 2007-05-09 04:30:58
我用的struts架构
jsp页面中:
<html:select property="interesting" multiple="true">
<html:option value="htmlselect.sing">Sing</html:option>
<html:option value="htmlselect.print">Print</html:option>
<html:option value="htmlselect.film">Film</html:option>
<html:option value="htmlselect.computerGame">Computer Game</html:option>
<html:option value="htmlselect.other">Other</html:option>
</html:select>
可以多选,对应的form中,我定义了一个与之对应的数组,可以自动获得值,问题是
自动获得的是我选中的value值,如:htmlselect.print,而我想要的是显示的文本值,如何得到呢。不能把value和显示文本写成一样的。
高手指点一下,分不多了,多谢。
...全文
793 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
backhamx7 2007-05-10
  • 打赏
  • 举报
回复
dreamover(梦醒了〖http://hellfire.cn〗)
_____________________________________
正解
zx116 2007-05-10
  • 打赏
  • 举报
回复
ArrayList arrayList = new ArrayList();

arrayList.add(new LabelValueBean("1001","name1"));
arrayList.add(new LabelValueBean("1002","name2"));
arrayList.add(new LabelValueBean("1003","name3"));

request.setAttribute("list",arrayList);
可以在Action里实现.


<html:select property="username">
<html:options collection="list" property="id" labelProperty="name" />
</html:select/>
bhwhy 2007-05-10
  • 打赏
  • 举报
回复
多谢各位。
我对js不熟悉,现在把select用options了,全部放到一个properties里面,value和text设定成一样的,不搞那末复杂了。也看不出什莫优势阿。
value和text不一样真的有优势吗
ouwaner 2007-05-10
  • 打赏
  • 举报
回复
我觉的可以变通一下 改为
jsp页面中:
<html:select property="interesting" multiple="true">
<html:option value="htmlselect.sing$Sing">Sing</html:option>
<html:option value="htmlselect.print$Print">Print</html:option>
<html:option value="htmlselect.film$Film">Film</html:option>
<html:option value="htmlselect.computerGame$Computer Game">Computer Game</html:option>
<html:option value="htmlselect.other$Other">Other</html:option>
</html:select>
然后根据传进的值是否indexOf(数组[i]) != -1 ,那么split('$')后的数组[1]就是得到的值了
dreamover 2007-05-10
  • 打赏
  • 举报
回复
<body>
<form>
<select name="select1">
<option value="v1">l1</option>
<option value="v2">l2</option>
</select>
<input type="hidden" name="hide1">
<input type="submit" onclick="document.all['hide1'].value=document.all['select1'].options[document.all['select1'].selectedIndex].text;">
</form>
</body>
bhwhy 2007-05-09
  • 打赏
  • 举报
回复
luyang1016(闭月羞花猫)
--------------
感觉有问题啊,我是想要得到显示的值,并不是如何显示的问题啊。呵呵
bhwhy 2007-05-09
  • 打赏
  • 举报
回复
ArrayList arrayList = new ArrayList();

arrayList.add(new LabelValueBean("1001","name1"));
arrayList.add(new LabelValueBean("1002","name2"));
arrayList.add(new LabelValueBean("1003","name3"));

request.setAttribute("list",arrayList);

------------------
在JSP页面如下
<html:select property="username">
<html:options collection="list" property="id" labelProperty="name" />
</html:select/>

这样可以实现,不过我是直接转到jsp页面,这句request.setAttribute("list",arrayList);
我应当放哪合适。不能放到JSP中
luyang1016 2007-05-09
  • 打赏
  • 举报
回复
首先你要定义个form。
form里面定义2个list, 分别用来表示codeList和nameList,
再定义个String 来表示所选择的值selectItem

然后在java程序里。
codeList.add("1");
codeList.add("2");
codeList.add("3");
codeList.add("4");
codeList.add("5");

nameList.add("Sing")
nameList.add("Print")
...
...

selectItem = “随便一个初始值”

然后就OK了

luyang1016 2007-05-09
  • 打赏
  • 举报
回复
你自己上网找找,这个是标准的<html:select>的写法
bhwhy 2007-05-09
  • 打赏
  • 举报
回复
luyang1016(闭月羞花猫)
-------------
有点糊涂
luyang1016 2007-05-09
  • 打赏
  • 举报
回复
首先用了struts的tag问题变得很简单了

<html:select size="1" styleClass="scrolltext" name="FolderManagerForm" property="avaiableFolder" style="width=100%" onchange="parentChange();return false;">
<html:options name="FolderManagerForm" labelProperty="dropFolderNameList" property="dropFolderList" />
</html:select>

这个就是标准的struts的实现方式
<html:select 中:
name="FolderManagerForm" 你的form的名字
property="avaiableFolder" 你改为“interesting”

<html:options 中:
name="FolderManagerForm" 你的form的名字
labelProperty="dropFolderNameList"
property="dropFolderList"

property,labelProperty 分别对应你下拉列表的List值。

property 在form中是一个ArrayList。用来表示code
labelProperty 在form中是一个ArrayList。用来表示名称
比如问题中
value="htmlselect.sing">Sing</html:option>
<html:option value="htmlselect.print">Print</html:option>
<html:option value="htmlselect.film">Film</html:option>
<html:option value="htmlselect.computerGame">Computer Game</html:option>
<html:option value="htmlselect.other">Other</html:option>

可以把Sing,Print。。。。。 放到一个ArrayList中,然后通过 labelProperty 表示出来



allenblade 2007-05-09
  • 打赏
  • 举报
回复
你把value值和key对应,这样就可以了,或者索性value和文本一样呵呵
allenblade 2007-05-09
  • 打赏
  • 举报
回复
同意楼上的,只能取value,把文本值写到资源文件,通过key来取得
bhwhy 2007-05-09
  • 打赏
  • 举报
回复
这个问题搞了半下午了,查了不少资料了,头也发昏了。哪位推荐一个较好的实现方式。
bhwhy 2007-05-09
  • 打赏
  • 举报
回复
有什莫方法可以得到显示的值吗。脑子也糊涂了
码未央 2007-05-09
  • 打赏
  • 举报
回复
html:select获得的只能是value

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧