jQuery inputとselect要素でchangeイベントを実行するサンプル

環境
Google Chrome 105.0.5195.127
Windows 10 Home 64bit
jquery 3.6.1

構文
$('input,select’).change(function(){処理コード})
input要素とselct要素で同じchange()を使ってイベント処理を実行します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('input,select').change(function(){
console.log('changeで要素が変更されました');
});
});
</script>
</head>
<body>
<input type="text" name="user" value="山田"><br><br>
<select>
<option>東京</option>
<option>大阪</option>
<option>福岡</option>
</select>
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('input,select').change(function(){ console.log('changeで要素が変更されました'); }); }); </script> </head> <body> <input type="text" name="user" value="山田"><br><br> <select> <option>東京</option> <option>大阪</option> <option>福岡</option> </select> </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
     $('input,select').change(function(){
        console.log('changeで要素が変更されました');
    });
});
</script>
</head>
<body>

<input type="text" name="user" value="山田"><br><br>
<select>
    <option>東京</option>
    <option>大阪</option>
    <option>福岡</option>
</select>
</body>
</html>

実行結果
inputテキストボックスに要素を入力しまたはselectボックスに項目を選択すると、
コンソールログに「changeで要素が変更されました」が出力されます。

jQuery

Posted by arkgame