jQuery :input 選擇器



jQuery 中的:input 選擇器用於選擇所有表單元素,例如<input>、<textarea>、<select> 和 <button> 元素。它包含所有與輸入相關的元素,例如文字欄位、單選按鈕、複選框等等。

語法

以下是 jQuery :input 選擇器的語法:

$(":input")

引數

:input 選擇器將選擇所有輸入元素。

示例

在下面的示例中,我們使用 jQuery :input 元素來選擇所有表單元素:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            // Change the background color of all input elements
            $(":input").css("background-color", "lightblue");
        });
    </script>
</head>
<body>
    <form>
        <input type="text" name="textfield" placeholder="Text Field" /><br>
        <textarea name="textarea" placeholder="Text Area"></textarea><br>
        <select name="select">
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
        </select><br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

選定的元素將以淺藍色背景突出顯示。

jquery_ref_selectors.htm
廣告
© . All rights reserved.