Home >Web Front-end >Front-end Q&A >How to set up jquery to trigger an event when the input value changes
In jquery, you can use the change() method to set an event to be triggered when the input value changes. This method is used to set a change event to occur when the value of the element changes. It can also specify a function to run when the event occurs. The syntax is "input element object.change (event trigger code)".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
The change event occurs when the value of the element changes (only applicable to form fields).
The change() method triggers a change event, or specifies a function to run when a change event occurs.
Note: When used with select elements, the change event occurs when an option is selected. When used with a text field or text area, the change event occurs when the element loses focus.
Syntax
Trigger the change event of the selected element:
$(selector).change()
Add a function to the change event:
$(selector).change(function)
function Optional. Specifies the function to be run when the change event occurs for the selected element.
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").change(function(){ alert("文本已被修改"); }); }); </script> </head> <body> <input type="text"> <p>在输入框写一些东西,然后按下 enter 键或点击输入框外部。</p> </body> </html>
Output result:
After modifying the text content:
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of How to set up jquery to trigger an event when the input value changes. For more information, please follow other related articles on the PHP Chinese website!