jQuery :checkbox 選擇器


jQuery :checkbox 選擇器用於選擇型別為 checkbox 的輸入元素。

語法

語法如下所示 -

$(":checkbox")

示例

讓我們看一個示例,在 jQuery 中實現 :checkbox 選擇器 -

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $(":checkbox").wrap("<span style='border: 2px green dashed'>");
   });
</script>
</head>
<body>
<h2>Fill the form</h2>
<form action="">
ID: <input type="text" name="id"><br>
Rank: <input type="text" name="rank"><br>
Fav. Subjects:
Maths: <input type="checkbox" name="sub" value="maths">
English <input type="checkbox" name="sub" value="english"><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>

輸出

這將生成以下輸出 -

示例

讓我們看另一個示例 -

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $(":checkbox").wrap("<span style='background-color: orange'>");
   });
</script>
</head>
<body>
<h2>Hobbies</h2>
<form action="">
Player Name: <input type="text" name="name"><br>
Fav Sports
Football: <input type="checkbox" name="sp" value="football">
Cricket <input type="checkbox" name="sp" value="cricket"><br><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>

輸出

這將生成以下輸出 -

更新於: 05-11-2019

361 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.