返回介绍

:radio Selector

发布于 2017-09-11 17:11:52 字数 2128 浏览 1123 评论 0 收藏 0

所属分类:选择器 > 表单 | 选择器 > jQuery 扩展

radio selector

描述: 选择所有类型为单选框的元素。

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

$(':radio')等价于$('[type=radio]')。如同其他伪类选择器(那些以“:”开始),建议使用此类选择器时,跟在一个标签名或者其它选择器后面,默认使用了全局通配符选择器 "*"。换句话说$(':radio') 等同于 $('*:radio'),所以应该使用$('input:radio')

要选择一个单选按钮相关的设置,你可以使用:$('input[name=gender]:radio')

Additional Notes(其他注意事项):

  • 因为:radio 是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用:radio 查询不能充分利用原生DOM提供的querySelectorAll() 方法来提高性能。为了当使用:radio 的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用[type="radio"]代替.

例子:

查找所有单选按钮。

<!DOCTYPE html>
<html>
<head>
  <style>
  textarea { height:25px; }
  </style>
  <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.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");
$("form").submit(function () { return false; }); // so it won't submit
</script>
 
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文