加载中...

:input


概述    input selector

返回值:Array<Element(s)>

描述:选择所有 input, textarea, select 和 button 元素.

  • V : 1.0jQuery( ":input" )

The :input 选择器基本上选择所有表单控件。

Additional Notes(其他注意事项):

  • 因为 :input 是 jQuery 延伸出来的一个选择器。并且不是的CSS规范的一部分, 使用:input()查询不能充分利用原生DOM提供的querySelectorAll() 方法来提高性能。为了在现代浏览器上获得更佳的性能,请使用 .filter(":input")代替。

示例

实例

查找所有的input元素,下面这些元素都会被匹配到。

HTML 代码:
  1. <form>
  2. <input type="button" value="Input Button"/>
  3. <input type="checkbox" />
  4. <input type="file" />
  5. <input type="hidden" />
  6. <input type="image" />
  7. <input type="password" />
  8. <input type="radio" />
  9. <input type="reset" />
  10. <input type="submit" />
  11. <input type="text" />
  12. <select><option>Option</option></select>
  13. <textarea></textarea>
  14. <button>Button</button>
  15. </form>
jQuery 代码:
  1. $(":input")
结果:
  1. [
  2. <input type="button" value="Input Button"/>,
  3. <input type="checkbox" />,
  4. <input type="file" />,
  5. <input type="hidden" />,
  6. <input type="image" />,
  7. <input type="password" />,
  8. <input type="radio" />,
  9. <input type="reset" />,
  10. <input type="submit" />,
  11. <input type="text" />,
  12. <select><option>Option</option></select>,
  13. <textarea></textarea>,
  14. <button>Button</button>,
  15. ]

实例

查找所有input元素.

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>input demo</title>
  6. <style>
  7. textarea {
  8. height: 25px;
  9. }
  10. </style>
  11. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  12. </head>
  13. <body>
  14. <form>
  15. <input type="button" value="Input Button">
  16. <input type="checkbox">
  17. <input type="file">
  18. <input type="hidden">
  19. <input type="image">
  20. <input type="password">
  21. <input type="radio">
  22. <input type="reset">
  23. <input type="submit">
  24. <input type="text">
  25. <select>
  26. <option>Option</option>
  27. </select>
  28. <textarea></textarea>
  29. <button>Button</button>
  30. </form>
  31. <div id="messages"></div>
  32. <script>
  33. var allInputs = $( ":input" );
  34. var formChildren = $( "form > *" );
  35. $( "#messages" ).text( "Found " + allInputs.length + " inputs and the form has " +
  36. formChildren.length + " children." );
  37. $( "form" ).submit(function( event ) {
  38. event.preventDefault();
  39. });
  40. </script>
  41. </body>
  42. </html>

运行一下


还没有评论.