Query判断radio单选框是否被选中,并获取选中值

function checkradio(){ 
var item = $(":radio:checked"); 
var len=item.length; 
if(len>0){ 
  alert("yes--选中的值为:"+$(":radio:checked").val()); 

还有一个更经典的例子

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>New Web Project</title>
  <script src="jquery-1.7.1.min.js"></script>
  <script type="text/javascript">
   window.onload= function(){
    var inpt= document.getElementById('myForm').getElementsByTagName('input');  //获取表单下所有的input元素
    for(var i=0;i<inpt.length;i++){   //遍历获得的input元素
     if(inpt[i].type=='radio'){   //如果是单选按钮
      if(inpt[i].defaultChecked)  //页面载入时选中的值
      document.getElementById('text1').value=inpt[i].nextSibling.nodeValue; //显示页面载入时选中的值
      inpt[i].onclick=function(){ // input的单击事件
       if(this.checked)
       document.getElementById('text2').value=this.nextSibling.nodeValue; //显示被选中的值
      }
     }
    }
   }
   // 下面是用jquery实现
   $(document).ready(function(){
    $('input:radio').each(function(){
     if(this.checked)
     $('#text1').val($(this).val());
     $(this).click(function(){
      if(this.checked)
      $('#text2').val($(this).val());
     });
    });
    
   });
  </script>
 </head>
 <body>
  <form id="myForm">
   <input type="radio" name="rad" checked="checked" value="音乐"/>音乐<br />
   <input type="radio" name="rad" value="美术"/>美术<br />
   <input type="radio" name="rad" value="电影"/>电影<br />
   默认值:<input type="text" id="text1" /><br />
   选中值:<input type="text" id="text2" />
  </form>
 </body>
</html>

posted @ 2015-08-10 18:14  叨叨的蜗牛  阅读(548)  评论(0编辑  收藏  举报