**
Ajax回显,大家有好的方法多多评论,一起进步,程序猿。
ajax是无序的。
**
<script type="text/javascript" src="<%=request.getContextPath() %>/bootstrap/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
<--2表数据回显-->
$(function(){
$.ajax({
url:"<%=request.getContextPath() %>/student/findType",
type:"post",
data:{},
dataType:"json",
success:function(r){
for (var i = 0; i < r.length; i++) {
$("#s_type").append("<option value="+r[i].tid+">"+r[i].tname+"</option>")
}
}
})
<--获取要查询的Id-->
var sid = "${param.sid}"
$.ajax({
url:"<%=request.getContextPath() %>/student/findById",
type:"post",
data:{"sid":sid},
dataType:"json",
success:function(r){
$("#sid").val(r.sid);
$("#sname").val(r.sname);
$("#spassword").val(r.spassword);
$("#sage").val(r.sage);
$("#sbirthday").val(r.sbirthday);
//单选框
//让r.sex等值的单选框选中
//赋值prop("checked"):获取属性checked的值
//属性选择器$("[属性名='值']")或者$("[属性名=值]")
$("[value="+r.ssex+"]").prop("checked",true);
//复选框
var h = r.shappy;//吃饭/睡觉
var arr=h.split(",")//拆分 返回一个数组
//通过属性value=arr[i]的复选框中
for (var i = 0; i < arr.length; i++) {
$("[value='"+arr[i]+"']").prop("checked",true);
}
$("[value="+r.tid+"]").prop("selected",true);
/* $("#s_type option:selected").val(r.type.tid);
$("#s_type option:selected").html(r.type.tname); */
}
})
})
</script>
</head>
<body>
<form action="student/updateStudent">
编号:<input type="text" value="" name="sid" id="sid" readonly="readonly"><br>
姓名:<input type="text" value="" name="sname" id="sname"><br>
密码:<input type="password" value="" name="spassword" id="spassword"><br>
年龄:<input type="text" value="" name="sage" id="sage"><br>
性别:<input type="radio" name="ssex" value="男">男
<input type="radio" name="ssex" value="女">女<br>
爱好:<input type="checkbox" name="shappy" value="吃饭">吃饭
<input type="checkbox" name="shappy" value="睡觉">睡觉
<input type="checkbox" name="shappy" value="玩耍">玩耍<br>
生日:<input type="date" name="sbirthday" id="sbirthday"><br>
学科:<select name="stype" id="s_type">
<option value="0">请选择</option>
</select><br>
<input type="submit" value="添加">
</form>
</body>
</html>``
student (1) 表
sid int
sname varchar
spassword varchar
sage int
ssex varchar
shappy varchar
sbirthday date
stype int
type (2)表
tid int
tname varchar