Home  >  Article  >  Web Front-end  >  How to set up jquery to trigger an event when the input value changes

How to set up jquery to trigger an event when the input value changes

WBOY
WBOYOriginal
2022-04-18 11:43:078542browse

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)".

How to set up jquery to trigger an event when the input value changes

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to set jquery to trigger an event when the input value changes

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:

How to set up jquery to trigger an event when the input value changes

After modifying the text content:

How to set up jquery to trigger an event when the input value changes

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn