如何得知 jQuery 中哪個單選按鈕被選中?


使用 jQuery val() 方法獲取選定單選按鈕的值。你可以嘗試執行以下程式碼以瞭解如何透過 jQuery 得知哪個單選按鈕被選中 −

示例

線上演示

<html>
   <head>
      <title>jQuery Selector</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(function(){
           $("#submit").click(function(){      
              alert($('input:radio:checked').val());
          });
         });
      </script>
   </head>
   <body>
      <form id="myForm">
         Select a number:<br>
         <input type="radio" name="q1" value="1">1
         <input type="radio" name="q1" value="2">2
         <input type="radio" name="q1" value="3">3<br>
         <button id="submit">Result</button>
      </form>
   </body>
</html>

更新時間: 13-2 月-2020

195 次瀏覽

開啟你的 職業

完成課程獲取證書

開始使用
廣告