单选框,复选框和下拉框回显赋值问题
下拉菜单
html:
<tr>
<td>是否销户</td>
<td>
<select name="brand.isexist">
<option value="0">--已销户--</option>
<option value="1">--在户--</option>
</select>
</td>
</tr>
jsp:
是否销户
<select name="brand.isexist" id="isexist">
<option value="0">--已销户--</option>
<option value="1">--在户--</option>
</select>
$("#isexist").val("${brand.isexist}");
单选框
html:
<tr>
<td>性别</td>
<td><input type="radio" name="brand.gender" value="0" checked="checked">男
<input type="radio" name="brand.gender"value="1">女
</td>
</tr>
jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
性别
<c:choose>
<c:when test="${brand.gender==0 }">
<input type="radio" name="brand.gender" value="0" checked="checked">男
<input type="radio" name="brand.gender" value="1">女
</c:when>
<c:when test="${brand.gender==1 }">
<input type="radio" name="brand.gender" value="0" >男
<input type="radio" name="brand.gender" value="1" checked="checked">女
</c:when>
</c:choose>
复选框:
html:
<tr>
<td>习惯</td>
<td><input type="checkbox" name="brand.hobby" value="吃饭" id="like">吃饭
<input type="checkbox" name="brand.hobby" value="睡觉" id="like">睡觉
<input type="checkbox" name="brand.hobby" value="打豆豆" id="like">打豆豆
</td>
</tr>
jsp:
//当页面加载完成的时候,自动调用
/* var like = '${brand.hobby}';
$("input[name='brand.hobby']").each(function(i,v){
if(like.indexOf($(v).val())>=0)
$(v).attr("checked","true")
}); */
window.onload=function(){
//获取所有复选框
var hobby = document.getElementsByName("brand.hobby");
//获取字符串,替换防止出现空格
var like = '${brand.hobby}'.replace(" ", ",");
//去掉,号
var pay = like.split(",");
for(var i=0;i<hobby.length;i++){
for(var j=0;j<pay.length;j++){
//如果值与修改前的值相等
if(hobby[i].value==pay[j]){
hobby[i].checked = true;
break;
}
}
}
};