1. 程式人生 > >JavaScript:單選鈕的事件處理

JavaScript:單選鈕的事件處理

<!doctype html>
<html lang="zh-CN">
  <head>
     <meta charset="utf-8">
     <meta name="description" content="all kinds of input">
     <meta name="keywords" content="input,html">

      <title>單選鈕的測試</title>

    <script type="text/javascript">
        window.onload = function () {
           document.getElementById('showBtn').addEventListener('click',function() {
                var sexArr = document.all('sex');
                if (sexArr[0].checked) {
                   alert("性別是:" + sexArr[0].value);
                }else{
                  alert("性別是:" + sexArr[1].value);
                }
                
           },false);
        }
    </script>

  </head>
    
  <body>
          <form action="">
               性別:<input type="radio"  name="sex" id="sex" value="male - 男" checked="yes">男
               <input type="radio"  name="sex" id="sex" value="female - 女">女<br/>
               <input type="button" id="showBtn" value="選擇"></input>
          </form>
  </body>
</html>