當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery :input用法及代碼示例

用法
input selector

說明:選擇所有輸入、文本區域、選擇和按鈕元素。

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

:input 選擇器本質上選擇所有表單控件。

其他注意事項:

  • 因為 :input 是 jQuery 擴展而不是 CSS 規範的一部分,所以使用 :input 的查詢無法利用本機 DOM querySelectorAll() 方法提供的性能提升。為了在使用 :input 選擇元素時獲得最佳性能,首先使用純 CSS 選擇器選擇元素,然後使用 .filter(":input")

例子:

查找所有輸入元素。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>input 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">
  <input type="reset">
  <input type="submit">
  <input type="text">
  <select>
    <option>Option</option>
  </select>
  <textarea></textarea>
  <button>Button</button>
</form>
<div id="messages"></div>
 
<script>
var allInputs = $( ":input" );
var formChildren = $( "form > *" );
$( "#messages" ).text( "Found " + allInputs.length + " inputs and the form has " +
  formChildren.length + " children." );
 
$( "form" ).submit(function( event ) {
  event.preventDefault();
});
</script>
 
</body>
</html>

演示:

相關用法


注:本文由純淨天空篩選整理自jquery.com大神的英文原創作品 :input。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。