jQuery:checked選擇器


jQuery中的:checked選擇器用於查詢並選中所有已選的單選按鈕或複選框。

語法

語法如下 −

$(":checked")

示例

下面讓我們實現jQuery中:checked選擇器 −

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h2>Favourite Subject</h2>
<form>
<div>
Maths: <input type="radio" name="sub" value="Maths">
</div>
<div>
English <input type="radio" name="sub" value="English">
</div><br>
<div id="content"></div>
</form>
<script>
   $( "input" ).on( "click", function() {
      $( "#content" ).html( $( "input:checked" ).val() + " is the favourite subject" );
   });
</script>
</body>
</html>

輸出

此操作將生成以下輸出 −

選中上面的“數學”單選按鈕 −

現在,選中“英語”單選按鈕 −

更新於:2019年11月05日

256次瀏覽

啟動你的事業

結業後獲得認證

開始學習
廣告