JSP的3种方式实现radio ,checkBox,select的默认选择值

JSP的3种方式实现radio ,checkBox,select的默认选择值。
以radiao 为例:
第一种方式:在jsp中使用java 脚本,这个方法最直接,不过脚本太多,不容易维护

<%String state = request.getParrameter("state" )%>
 <td width="27"><input type="radio" name="state" value="AL"〈%if(state.equal("AL")) out.print("checked")%〉/>
 <td width="27"><input type="radio" name="state" value="MT"〈%if(state.equal("MT")) out.print("checked")%〉/> </td>

第二种方式:使用jsp标准标签库jstl:
首先需要加入库文件

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>其次为:<td width="27"><c:choose>                     <c:when test="${state=='AL'}">                     <input type="radio" name="state" value="AL"  checked />                     </c:when>                     <c:otherwise>                      <input type="radio" name="state" value="AL"  />                     </c:otherwise>                     </c:choose>                    <c:choose>
 <td width="27"><c:when test="${state=='MT'}">                     <input type="radio" name="state" value="MT"  checked />                     </c:when>                     <c:otherwise>                      <input type="radio" name="state" value="MT"  />                     </c:otherwise>                     </c:choose>                    <c:choose> </td>


其中state应该在request,session,等中赋值。
第三种方式:使用EL表达式:

<td width="27"><input type="radio" name="state" value="AL" ${(state=='AL')?'checked' : ''}/>
 <td width="27"><input type="radio" name="state" value="MT" ${(state=='MT')?'checked' : ''}/> </td>

显然使用EL表达式这种方式最简洁,不用写多余的代码。


转载于:https://my.oschina.net/yunjie/blog/659707

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值