jQuery changeメソッドを使ってテキストフォームの変更を取得するサンプル

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

構文
$(“テキストフォームのセレクターID名").change(function () {処理コード}
changeメソッドを使ってテキストフォームの変更を取得します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border:1px solid black;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(function(){
//テキストフォームの値を変更する
$("#txt").change(function () {
$("#result").text("入力した値: "+$("#txt").val())
});
})
</script>
</head>
<body>
<input id="txt" type="text" placeholder="名前を入力してください">
<div id="result"></div>
</body>
</html>
<!DOCTYPE html> <html> <head> <style> table, th, td { border:1px solid black; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> $(function(){ //テキストフォームの値を変更する $("#txt").change(function () { $("#result").text("入力した値: "+$("#txt").val()) }); }) </script> </head> <body> <input id="txt" type="text" placeholder="名前を入力してください"> <div id="result"></div> </body> </html>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border:1px solid black;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(function(){ 
   //テキストフォームの値を変更する
  $("#txt").change(function () {
      $("#result").text("入力した値: "+$("#txt").val())

    });
})
</script>
</head>
<body>
 <input id="txt" type="text" placeholder="名前を入力してください">
 <div id="result"></div>
</body>
</html>

実行結果
テキストフォームに「yamada」と入力し、「入力した値: yamnada」が表示されます。

jQuery

Posted by arkgame