input-Selektor
Der Selektor :input wählt Formularelemente
aus - Inputs, Textareas, Dropdown-Listen und
Buttons. Siehe Tags:
input,
textarea,
button,
select.
Da :input nicht zur CSS-Spezifikation gehört,
ist es für eine bessere Leistung in modernen
Browsern besser, zuerst die Elemente mit
einem reinen CSS-Selektor zu filtern und dann
.filter(':input') anzuwenden.
Syntax
So wählen wir Formularelemente aus:
$(':input');
Beispiel
Lassen Sie uns gemäß der oben genannten Theorie
alle Formularelemente auswählen und ihre
Anzahl mithilfe der Eigenschaft
length in der Konsole ausgeben. Wir
werden sehen, dass alle 13 Tags erfasst wurden:
<form>
<input type="button" value="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>
textarea {
height: 25px;
}
let allInputs = $(':input');
console.log('Tags amount: ' + allInputs.length);
$('form').submit(function(event) {
event.preventDefault(); // verhindert das Senden des Formulars
});