獲取單選按鈕選中的值


在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