Home > Web Front-end > JS Tutorial > body text

js and jquery real-time monitoring of the oninput and onpropertychange methods of the input box value

高洛峰
Release: 2017-02-03 14:10:30
Original
1880 people have browsed it

The example of this article describes the oninput and onpropertychange methods of js and jquery that monitor the input box value in real time. Share it with everyone for your reference. The details are as follows:

I recently worked on a project, and the requirement was to automatically match keywords in the drop-down box. The specific details were to monitor changes in the value of the text box in real time, and then match related content.

When I first took over the project, the first thing I thought of was change in JQ, but I immediately ruled out this method because change is triggered when the text box loses focus. To save the country, I thought of using keydown to solve it. Everything else is fine, except that this event cannot be triggered when copying and pasting using the mouse instead of using the keyboard. So this method is also eliminated.

Then, I checked some relevant information and found that only oninput & onpropertychange of native js met this requirement. Then I went to JQ's API to find a matching method. I was disappointed that I couldn't find it, but bind does. Binding similar events, that is, input & propertychange, passed the test and it was indeed no problem.

Now here is an example:

JQ:

$('input').bind('input propertychange', function() {
 //进行相关操作
});
Copy after login

Among them: propertychange is for compatibility with versions below IE9.

The oninput event in JS is not supported in versions below IE9. It needs to be replaced by IE's unique onpropertychange event. This event will be triggered when the user interface changes or when the content is directly modified using a script. There are the following types Situation:

The selected state of the input:checkbox or input:radio element is modified, and the checked attribute changes.

The value of the input:text or textarea element is modified, and the value attribute changes.
The selected item of the select element has been modified, and the selectedIndex attribute has changed.
JS:

if(isIE)
{
 document.getElementById("input").onpropertychange = keys();
}
else //需要用addEventListener来注册事件
{
 document.getElementById("input").addEventListener("input", keys, false);
}
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

For more articles related to js and jquery’s oninput and onpropertychange methods for real-time monitoring of input box values, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!