html:radio 使用心得

本文探讨了在Struts框架中使用html:radio标签实现性别选择时遇到的动态选中问题,并提供了解决方案。通过设置value属性与formbean属性匹配,确保正确的选项被选中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近在做struts的时候用到了,html:radio来表示性别。但是在使用的过程中遇到了不能动态选中的问题。后来在网上查找后基本的解决方案是:

 

Each radio button element requires an assigned value that distinguishes it from the other radio buttons. When creating a static array of radio buttons, you need to indicate which one of these, if any, is checked. This does not need to be done when the radio buttons are being populated from dynamic data. The control can compare itself to the form bean's property and then check itself when appropriate.

Given a set of 
< html:radio >  controls like this:

< html:radio  property ="expectedVia"  value ="UPS" /> UPS 
< html:radio  property ="expectedVia"  value ="FEDX" /> Federal Express 
< html:radio  property ="expectedVia"  value ="AIRB" /> AirBorne
And that the expectedVia property on the form bean was already set to "UPS", then the HTML radio elements would be rendered like this:

< input  type ="radio"  name ="expectedVia"  value ="UPS"  checked ="checked" > UPS 
< input  type ="radio"  name ="expectedVia"  value ="FEDX" > Federal Express 
< input  type ="radio"  name ="expectedVia"  value ="AIRB"   > AirBorne
If you need to create a dynamic set of radio buttons, or want to localize the values, you can create in an Action a collection of LabelValueBeans with the appropriate labels and values for each button. Here's an example:

ArrayList shippers = new ArrayList(); 
  shippers.add(new LabelValueBean("UPS", "UPS")); 
  shippers.add(new LabelValueBean("Federal Express", "FEDX")); 
  shippers.add(new LabelValueBean("AirBorne", "AIRB")); 
request.setAttribute ("SHIPPERS",shippers);
Then, on the page, you can iterate through the collection

< logic:iterate  id ="row"  name ="SHIPPERS"  type ="org.apache.commons.scaffold.util.LabelValueBean" >  
  
< html:radio  property ="expectedVia"  value ="<%=row.getValue()%>" />  
  
< bean:write  name ="row"  property ="label" />  
</ logic:iterate >
So long as one of the values matches the "expectedVia" property on our ActionForm, the radio tag will still automatically select the appropriate button.

 

上面的解释可以大概概括如下:

1.如果是固定的值,比如性别:男,女。你只需把value的属性设置为各自的值。然后当页面显示的时候会自动把从frombean中对应的数据给选中。

2.如果你是动态生成的radio,就用logic:iterate就可以了。

但是我按上述的第一中方法编写如下jsp代码:

 

< html:radio  value ="M"  property ="sex"  name ="member" > </ html:radio >
< html:radio  property ="sex"  value ="F"  name ="member" > </ html:radio >

 

action过来的form bean中也设置了sex的值。但是radio就是不选中。

后来没办法,只能用java script来实现了。

 

< script type = " text/javascript " >
function  setChecked(value)
if(value=="M")
{                               
     document.forms[
0].sex[0].checked=true;  
}
 
else
{                                                         
     document.forms[
0].sex[1].checked=true
}

</script>
其中memberInfo是该页面中引用form bean的<bean:define的Id。

 

然后在jsp最后加上下面的代码:

 

< script type = " text/javascript " >
            setChecked(
<%= memberInfo.getSex() %> )
</ script >

 

这样设置后运行正常!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值