复选框,单选框及各种类型的ajax回显

**

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
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值