select下拉菜单与button的互动选择

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>select下拉菜单与button的互动选择</title>
<script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>
<style>
	select{ width:100px; height:30px; line-height:30px; border:#ccc 1px solid; border-radius:3px;}
	.btn{width:100px; height:30px; line-height:30px; background:#333; color:#fff; border:none; border-radius:3px;}
	.btn.xuanz{ background:#09F;}
</style>
</head>

<body>
  <label for="caidan"></label>
  <select name="caidan" id="caidan">
    <option>温江区</option>
    <option>双流区</option>
    <option>龙泉驿区</option>
    <option>新都区</option>
    <option>郫县</option>
    <option>青白江区</option>
  </select>
  	<button class="btn">温江区</button>
    <button class="btn">双流区</button>
    <button class="btn">龙泉驿区</button>
    <button class="btn">新都区</button>
    <button class="btn">郫县</button>
    <button class="btn">青白江区</button>
<script>
/*法一、
var ablumlistnumeber=$("#caidan option").length;
$("#btn1").addClass("xuanz");
for(i=0; i<ablumlistnumeber;i++){
	$("#btn"+(i+1)).click(function(){
		 var cityname=$(this).val();
		 $(this).siblings().removeClass("xuanz");
		 $(this).addClass("xuanz");
		 $("#caidan").children("option").each(function() {
			if($(this).text()==cityname)
			{	
			$(this).siblings().removeAttr("selected");
			$(this).attr("selected",true).prop("selected",true);
			//$(this).attr("selected",true).siblings().removeAttr("selected");
			} 
		})
	})	
}
$("#caidan").change(function(){
	//if($(this).find("option:selected")){
		var xh=$(this).children("option:selected").index();
		$("#btn"+(xh+1)).siblings().removeClass("xuanz");
		$("#btn"+(xh+1)).addClass("xuanz");
		//}
})
*/	
</script>	
<script>
//法二、
$(".btn").eq(0).addClass('xuanz')
$(".btn").click(function(){
	$(this).addClass('xuanz').siblings('.btn').removeClass('xuanz')
	$("#caidan").find("option").eq($(".btn").index($(this))).prop("selected",true) //$(this).index() 也可以找下标,prop和attr效果是一样的,无太大区别
})
$("#caidan").change(function(){
	alert($('#caidan option').index($('#caidan option:selected')))//获取索引
	$(".btn").eq($('#caidan option').index($('#caidan option:selected'))).addClass('xuanz').siblings('.btn').removeClass('xuanz')
})
</script>
</body>
</html>

转载于:http://www.qdfuns.com/notes/22982/fcfce883a7f481aa3d8731bf7b8a340f.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值