• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • :radio

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

    jQuery(":radio")

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

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

    注意

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

    例子

    查找所有单选按钮。

    <!DOCTYPE html>
    <html>
    <head>
      <style>
      textarea { height:25px; }
      </style>
      <script src="https://www.lanmper.cn/static/js/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");
    $("form").submit(function () { return false; }); // so it won't submit
    </script>
     
    </body>
    </html>
    

    上篇::password

    下篇::reset