使用js更改抛出事件的输入值

时间:2022-12-03 07:53:45

I've an input field that can be modify by users and js buttons. I need to throw an event when the user writes/deletes something in the textarea and when a button modifies the value of the input without using jQuery.

我有一个输入字段,可以由用户和js按钮修改。当用户在textarea中写入/删除某些内容时,以及当一个按钮在不使用jQuery的情况下修改输入值时,我需要抛出一个事件。

I tried to overwrite input's get and set methods but when I change input text with js before modify it by my hand and change it again with js the displayed text in the input box doesn't change.

我试图覆盖输入的get和set方法但是当我用js更改输入文本然后用我的手修改它并用js再次更改它时输入框中显示的文本不会改变。

It has to work on IE11 and Chrome (cannot use onpropertychange event).

它必须适用于IE11和Chrome(不能使用onpropertychange事件)。

I'm sorry for my bad english but I hope I was clear.

我很抱歉我的英语不好但我希望我很清楚。

`https://jsfiddle.net/gzmdc3zz/1/`

1 个解决方案

#1


0  

Well there is an 'oninput' and an 'onchange' in javascript. Try one of these:

好吧,javascript中有'oninput'和'onchange'。尝试以下方法之一:

object.oninput=function(){myScript};

Edit: I think there is no true solution for this, but you can try the hack on this link instead of your manipulated setters/getters. In this case you get an event-callback whenever the given property of an element has changed programmatically (with new and old value).

编辑:我认为没有真正的解决方案,但你可以尝试在这个链接上的黑客而不是你操纵的setter / getters。在这种情况下,只要元素的给定属性以编程方式更改(使用新旧值),就会获得事件回调。

You can then change the value on the original element by yourself or do whatever you want. In addition you then need a normal .oninput-event on the element,too. To get the event from user inputs.

然后,您可以自己更改原始元素的值或执行您想要的任何操作。此外,您还需要在元素上使用正常的.oninput事件。从用户输入获取事件。

If anyone has a better idea how to manage this, I'm looking forward to read it.

如果有人知道如何管理这个,我期待着阅读它。

#1


0  

Well there is an 'oninput' and an 'onchange' in javascript. Try one of these:

好吧,javascript中有'oninput'和'onchange'。尝试以下方法之一:

object.oninput=function(){myScript};

Edit: I think there is no true solution for this, but you can try the hack on this link instead of your manipulated setters/getters. In this case you get an event-callback whenever the given property of an element has changed programmatically (with new and old value).

编辑:我认为没有真正的解决方案,但你可以尝试在这个链接上的黑客而不是你操纵的setter / getters。在这种情况下,只要元素的给定属性以编程方式更改(使用新旧值),就会获得事件回调。

You can then change the value on the original element by yourself or do whatever you want. In addition you then need a normal .oninput-event on the element,too. To get the event from user inputs.

然后,您可以自己更改原始元素的值或执行您想要的任何操作。此外,您还需要在元素上使用正常的.oninput事件。从用户输入获取事件。

If anyone has a better idea how to manage this, I'm looking forward to read it.

如果有人知道如何管理这个,我期待着阅读它。