获取单选按钮选中的值


在html中,页面只有这些代码

<script type="text/javascript">

   function ok(){

    //获取所有单选按钮(同一组),得到对象
    var fruits = document.getElementByName("fruit");

    for(var i=0;i<fruits.length;i++){

      if(fruits[i].checked){

        alert("选择了"+fruits[i].value);

      }

    }

  }

</script>

<body>

<div>
    <input type="radio"  name ="fruit" value="apple" onclick="ok()">苹果
    <input type="radio"  name ="fruit" value="banana" onclick="ok()" >香蕉
    <input type="radio"  name ="fruit" value="pear" onclick="ok()">梨
</div>

</body>

页面显示情况是这样的:

选中时,情况是这样的:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM