当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JQuery :radio用法及代码示例


用法
radio selector

说明:选择所有无线电类型的元素。

  • 添加的版本:1.0jQuery( ":radio" )

$( ":radio" ) 等价于 $( "[type=radio]" ) 。与其他 pseudo-class 选择器(以 ":" 开头的选择器)一样,建议在其前面加上标签名称或其他选择器;否则,将隐含通用选择器 ("*")。换句话说,裸露的 $( ":radio" ) 等同于 $( "*:radio" ) ,所以应该使用 $( "input:radio" ) 代替。

要选择一组关联的单选按钮,您可以使用:$( "input[name=gender]:radio" )

其他注意事项:

  • 因为 :radio 是 jQuery 扩展而不是 CSS 规范的一部分,所以使用 :radio 的查询无法利用本机 DOM querySelectorAll() 方法提供的性能提升。为了在现代浏览器中获得更好的性能,请改用[type="radio"]

例子:

查找所有无线电输入。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>radio demo</title>
  <style>
  textarea {
    height: 25px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<form>
  <input type="button" value="Input Button">
  <input type="checkbox">
  <input type="file">
  <input type="hidden">
  <input type="image">
  <input type="password">
  <input type="radio" name="asdf">
  <input type="radio" name="asdf">
  <input type="reset">
  <input type="submit">
  <input type="text">
  <select>
    <option>Option</option>
  </select>
  <textarea></textarea>
  <button>Button</button>
</form>
<div></div>
 
<script>
var input = $( "form input:radio" )
  .wrap( "<span></span>" )
  .parent()
    .css({
      background: "yellow",
      border: "3px red solid"
    });
 
$( "div" )
  .text( "For this type jQuery found " + input.length + "." )
  .css( "color", "red" );
 
// Prevent form submission
$( "form" ).submit(function( event ) {
  event.preventDefault();
});
</script>
 
</body>
</html>

演示:

相关用法


注:本文由纯净天空筛选整理自jquery.com大神的英文原创作品 :radio。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。