jquery操作input标签

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Untitled Document</title>
		<script type="text/javascript" src="../js/jquery-1.4.2.js"></script>
		<script type="text/javascript" >
			window.οnlοad=function(){
				//得到所有的按钮
				var $buttons = $("input[type=button]");
				//使单选下拉框的'2号'被选中
				$buttons.eq(0).click(function(){
					//方法一:
					//$("#one option").eq(1).attr("selected",true);
					//方法二:
					//$("#one").val("2号");
					//方法三:
					$("#one").children().eq(1).attr("selected",true);
				});
				//使多选下拉框选中的'2号'和'5号'被选中
				$buttons.eq(1).click(function(){
					//$("#many").val(["2号","5号"]);  //方法一
					$("#many option").attr("selected",null); //先清空其他的选项	
					$("#many option").eq(1).attr("selected",true);
					$("#many option").eq(4).attr("selected",true);
				});
				//使复选框的'复选2'和'复选4'被选中
				$buttons.eq(2).click(function(){
					$("input[name=c]").eq(1).attr("checked",true);
					$("input[name=c]").eq(3).attr("checked",true);
				});
				
				//使单选框的'单选2'被选中
				$buttons.eq(3).click(function(){
					$("input[name=r]").eq(1).attr("checked",true);
				});
				
				//打印已经被选中的值***
				$buttons.eq(4).click(function(){
					$("select option:selected").each(function(){
						alert($(this).val());
					});
					//带两个条件的写法,按照题意只要去掉[name=c]这个条件即可
					$("input[type=checkbox][name=c]:checked").each(function(){
						alert($(this).val());
					});
				});
			}
			
		</script>
	</head>
	<body>
		<input type="button" value="使单选下拉框的'2号'被选中"/><br>
		<input type="button" value="使多选下拉框选中的'2号'和'5号'被选中"/><br>
		<input type="button" value="使复选框的'复选2'和'复选4'被选中"/><br>
		<input type="button" value="使单选框的'单选2'被选中"/><br>
		<input type="button" value="打印已经被选中的值"><br>
		<br/>
		
		<select id="one">
		  <option>1号</option>
		  <option>2号</option>
		  <option>3号</option>
		</select>
		
		<select id="many" multiple="multiple" style="height:120px;">
		  <option selected="selected">1号</option>
		  <option>2号</option>
		  <option>3号</option>
		  <option>4号</option>
		  <option>5号</option>
		  <option selected="selected">6号</option>
		</select>
		<br/>
		<br/>

		<input type="checkbox" name="c" value="check1"/> 复选1
		<input type="checkbox" name="c" value="check2"/> 复选2
		<input type="checkbox" name="c" value="check3"/> 复选3
		<input type="checkbox" name="c" value="check4"/> 复选4
		<br/>
		<input type="radio" name="r" value="radio1"/> 单选1
		<input type="radio" name="r"  value="radio2"/> 单选2
		<input type="radio" name="r"  value="radio3"/> 单选3
	</body>
</html>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值