javascript 为什么 onchange 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3773785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:11:43  来源:igfitidea点击:

Why onchange doesn't work?

javascripthtml

提问by Eureka

I'm just looking for an answer on the net but I haven't reached any one. I'm playing with google's api translation and I have a problem with JS. In fact my code is 100% ok except one operation.

我只是在网上寻找答案,但我还没有找到任何人。我在玩谷歌的 api 翻译,我有一个 JS 问题。事实上,除了一项操作外,我的代码是 100% 好的。

My problem is that the translation is not given at the time. After js sends the information to google it doesn't wait until the translation is given. Instead of this it continues reading my code so it doesn't stop for get an answer. It proceess the information to be translated and then the answer will be get some time after. So because I want to do translations of translations I have done a sweet loop. Because I don't know how to stop js, I have done a page with diferent inputs text box and in my loop when the data is received it's send to the value of the text box and here is where it becomes the problem. I want to enter to my js function again when the text is changed so If js changes the value it will return to my loop when I get the translation. Simply and effective but if I put in the input box:

我的问题是当时没有给出翻译。js 将信息发送给 google 后,它不会等到给出翻译。取而代之的是,它继续阅读我的代码,因此它不会停下来寻找答案。它处理要翻译的信息,然后在一段时间后得到答案。所以因为我想做翻译的翻译,所以我做了一个甜蜜的循环。因为我不知道如何停止 js,所以我做了一个带有不同输入文本框的页面,并且在我的循环中,当接收到数据时,它被发送到文本框的值,这就是问题所在。我想在更改文本时再次进入我的 js 函数,因此如果 js 更改了值,它将在我获得翻译时返回到我的循环。简单而有效,但如果我在输入框中输入:

onchange="myfunction()"

the loop doesn't work. But the strangest is that if I manually change the text then the function works so it's as if onchange only works when a human change the text but not if js change the text. Is there any solution? May be some dinamic listener or something like that?

循环不起作用。但最奇怪的是,如果我手动更改文本,则该函数会起作用,因此就好像 onchange 仅在人类更改文本时才起作用,而在 js 更改文本时不起作用。有什么解决办法吗?可能是一些动态的听众或类似的东西?

回答by Quentin

The onchangeevent only fires if the user changes the value of the input. It isn't supposed to fire if the input is changed programmaticly.

onchange事件仅在用户更改输入值时触发。如果以编程方式更改输入,则不应触发。

Call the function from whatever function sets the value instead.

从任何设置值的函数调用该函数。

回答by Ivan Feri?

The onchangeevent is designed to fire when the user (and only the user) changes the value and for that reason it is fired only after the value was changed ANDfield lost focus.

onchange活动旨在为火,当用户(和只有用户)的变化值,由于这个原因后,才值改变了它被激发领域失去焦点。

回答by vol7ron

Helpful Starters and that Bucle you speak of

有用的初学者和你说的那个Bucle



  1. You should look at the onkeyupor onblur.
  2. If the input value is being set by JavaScript, then why not call the event after setting it?
  1. 你应该看看onkeyupor onblur
  2. 如果输入值是由 JavaScript 设置的,那为什么不设置后调用事件呢?

I have no idea what a bucle is.
My best guess: it is Spanish for 'loop'

我不知道 bucle 是什么。
我最好的猜测:“循环”是西班牙语