即使通过 jquery 更改值,jquery 输入字段也会更改
我试图捕获文本输入字段的值何时更改。我正在使用:
$("input[name=color]").change(function(){
如果我直接输入新值,这会起作用。然而,输入字段的值正在被 jquery 更改。其中有几个字段,我需要知道任何字段何时发生更改。有没有办法检测到这种变化?
抱歉,我没说清楚。我不是改变价值的人。这是一个插件,我不想修改它,以防我需要将其移植到另一个项目。
解决方法
好的,所以你不能做我想做的事情,但这里有一个解决方法。我只是做了一个间隔并将我的 change
事件更改为 each
事件。
setInterval(function(){
$("input[name=color]").each(function(){ my code })
},100);
I am attempting to capture when a text input field's value is changed. I am using:
$("input[name=color]").change(function(){
This works if I type the new value in directly. However, the value of the input field is being changed by jquery. There are several of these fields, and I need to know when any have changed. Is there a way to detect this change?
Sorry, I wasn't clear. I'm not the one changing the value. It's an addon that I would rather not modify in case I need to port it to another project.
WORK-AROUND
Ok, so you can't do what I wanted to do, but here is a work-around. I just made an interval and changed my change
event to an each
event.
setInterval(function(){
$("input[name=color]").each(function(){ my code })
},100);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无法检测以编程方式完成的更改。
但是,只要修改其值,您就可以触发
change
事件。There is no way to detect the changes done programmatically.
However you can trigger a
change
event whenever you modify its value.当通过 jquery 更改值时,您可以随后使用 jquery 触发更改事件
When change the value by jquery you can fire the change event with jquery afterwards
不,使用 javascript 更改值时不会触发 onchange 事件。或者,您可以在更改值时调用 onchange 事件。
No, onchange event will not be triggered when the value is changed using javascript. Alternatively you can call the onchange event when you change the value.
一种选择是跟踪侦听器范围之外的值
One option is to track the value outside the scope of your listener