如何通过 jQuery 判断哪个单选按钮被选中?

jqueryweb developmentfront end technology更新于 2026/1/16 4:07:17

使用 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>

相关文章