js_前台页面如何设置radio、checkbox、select三者的值以及绑定事件,方便后台接收数据

servlet服务器后端根据前台form的标签的name接收数据

如:

input:text

input:radio

select

等,只需要设置name属性,并且给value属性赋值,后台自动接收(这里建议servlet使用String类型变量接收,在服务器端做解析,封装为其他类型)


input:checkbox属性比较特殊,需要使用js函数设置value值。

关于动态添加的节点,需要使用live绑定事件,用bind会无效。


以下提供一个demo参考:


<html>
	<head>
		<title>radio checkbox select</title>
		<style type="text/javascript">	
		</style>
		<script type="text/javascript" src="jquery-1.4.4.js">
		</script>
		<script>
			$(function(){
				//为所有的checkbox绑定change事件,方便获取value
				$(":checkbox").live("change",function(event){
					setChkVal(event);
				});
				
				//仅仅为了测试动态添加节点
				$(":radio").bind("change",function(event){
					setRadVal(event);
				});
				
				$("select").bind("change",function(event){
					alert($(event.target).val());
				});
			});
			
			function setRadVal(event){
				var dom = event.target;
				var id = $(dom).attr("id");
				var val = $(dom).val();
				var chk = $("#"+id).attr("checked");
				if(chk == true && val === "1"){
					$("div:eq(1)").empty();
					$("div:eq(1)").append(
					"<input type='checkbox' id='fav1' name='fav1' value='0'>"+
					"<label for='fav1' style='cursor:hand'>html</label>"
					);
					$("div:eq(1)").append("<input type='checkbox' id='fav2' name='fav2' value='1'>"+
					"<label for='fav2' style='cursor:hand'>css</label>"
					);
					return;
				}else{
					$("div:eq(1)").empty();
					if(val === "0" && chk == true){
						$("div:eq(1)").append("<input type='checkbox' id='fav1' name='fav1' value='0'>"+
						"<label for='fav1' style='cursor:hand'>java</label>"
						);
						$("div:eq(1)").append("<input type='checkbox' id='fav2' name='fav2' value='0'>"+
						"<label for='fav2' style='cursor:hand'>javascript</label>"
						);
						$("div:eq(1)").append("<input type='checkbox' id='fav3' name='fav3' value='0'>"+
						"<label for='fav3' style='cursor:hand'>sql</label>"
						);
						$("div:eq(1)").append("<input type='checkbox' id='fav4' name='fav4' value='0'>"+
						"<label for='fav4' style='cursor:hand'>no-sql</label>"
						);	
					}
				}
			}
			
			function setChkVal(event){
				var dom = event.target;
				var id = $(dom).attr("id"); 
				var chk = $("#"+id).attr("checked");
				if(chk == true){
					$("#"+id).val("1");
				}else{
					$("#"+id).val("0");
				}
				//alert($("#"+id).val());
			}
		</script>
	</head>
	<body>
		<h1>radio</h1>
		<div>
		性别:
		<input type="radio" id="male" name="sex" value="0" checked="checked"><label for="male" style="cursor:hand">男</label>
		<input type="radio" id="female" name="sex" value="1"><label for="female" style="cursor:hand">女</label>
		</div>
		<br/>
		<h1>checkbox</h1>
		爱好:
		<div>
		<input type="checkbox" id="fav1" name="fav1" value="0" checked=tr><label for="fav1" style="cursor:hand">java</label>
		<input type="checkbox" id="fav2" name="fav2" value="0"><label for="fav2" style="cursor:hand">javascript</label>
		<input type="checkbox" id="fav3" name="fav3" value="0"><label for="fav3" style="cursor:hand">sql</label>
		<input type="checkbox" id="fav4" name="fav4" value="0"><label for="fav4" style="cursor:hand">no-sql</label>
		</div>
		<br/>
		<h1>select</h1>
		<div>
		<label>城市:</label><select name="city">
			<option value="0" >南京</option>
			<option value="1" >苏州</option>
			<option value="2" >上海</option>
			<option value="3" >普罗旺斯</option>
		</select>
		</div>
	</body>
</html>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值