jquery radio 取值

网上流行的说法就是

$(input[name='aaa'][checked]).val()

能取到选中项的value,但我测试后发现只在IE下有效,在firefox和Chrome中不论选中哪一项,或者不选,取到的值都是第一项的value
正确做法应该是

$("input[name='aaa']:checked").val()

同样对于checkbox也是这种写法

 <html>
<head>
 
<script type="text/javascript" src="ui/jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(function(){
	$('#go').click(function(){
		var radio = $('input[name="testradio"]').filter(':checked');
		if(radio.length)
		alert(radio.val());
		else
		alert('请选择一个radio');
		
	});
	$('#go2').click(function(){
		$('input[name="testradio"]').each(function(){
			alert(this.value);
		});
	})
	$('#go3').click(function(){
		alert($('input[name="testradio"]:eq(1)').val());
	})
	
	$('#go4').click(function(){
		 
		 alert($("input[name='testradio']:checked").val());
		
	});
})
</script>

</head>

<body>
<input type="radio" name="testradio" value="radio1" 
/>jquery获取radio的值<br /> <input type="radio" name="testradio"
 value="radio2" />jquery获取checkbox的值<br /> 
<input type="radio" name="testradio" value="radio3" 
/>jquery获取select的值<br />
<button id="go">选中的那个radio的值</button>
<button id="go2">遍历所有radio的值</button>
<button id="go3">取第二个radio的值</button>

<button id="go4">取第</button>
</body>
</html>

 

posted @ 2013-07-25 08:24  StormeR  阅读(429)  评论(0编辑  收藏  举报