在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>

页面显示情况是这样的:

获取单选按钮选中的值

选中时,情况是这样的:

获取单选按钮选中的值

相关文章:

  • 2021-11-26
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2021-11-10
  • 2022-12-23
猜你喜欢
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2021-11-22
相关资源
相似解决方案