jQuery - 检测隐藏输入字段的值变化
我有一个隐藏文本字段,其值通过 AJAX 响应进行更新。
<input type="hidden" value="" name="userid" id="useid" />
当这个值改变时,我想触发一个 AJAX 请求。谁能建议如何检测变化?
我有以下代码,但不知道如何查找该值:
$('#userid').change( function() {
alert('Change!');
})
I have a hidden text field whose value gets updated via an AJAX response.
<input type="hidden" value="" name="userid" id="useid" />
When this value changes, I would like to fire an AJAX request. Can anyone advise on how to detect the change?
I have the following code, but do not know how to look for the value:
$('#userid').change( function() {
alert('Change!');
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
所以这已经太晚了,但我已经找到了答案,以防它对遇到此线程的任何人有用。
隐藏元素值的更改不会自动触发 .change() 事件。因此,无论您在何处设置该值,您还必须告诉 jQuery 触发它。
一旦出现这种情况,
应该会按预期工作。
So this is way late, but I've discovered an answer, in case it becomes useful to anyone who comes across this thread.
Changes in value to hidden elements don't automatically fire the .change() event. So, wherever it is that you're setting that value, you also have to tell jQuery to trigger it.
Once that's the case,
should work as expected.
由于隐藏输入不会在更改时触发“更改”事件,因此我使用 MutationObserver 来触发此事件。
(有时隐藏的输入值更改是由一些您无法修改的其他脚本完成的)
这在 IE10 及以下版本中不起作用
Since hidden input does not trigger "change" event on change, I used MutationObserver to trigger this instead.
(Sometimes hidden input value changes are done by some other scripts you can't modify)
This does not work in IE10 and below
您可以简单地使用下面的函数,您还可以更改类型元素。
隐藏元素值的更改不会自动触发 .change() 事件。因此,无论您在何处设置该值,您还必须告诉 jQuery 触发它。
HTML
JAVASCRIPT
应该按预期工作。
http://jsfiddle.net/7CM6k/3/
You can simply use the below function, You can also change the type element.
Changes in value to hidden elements don't automatically fire the .change() event. So, wherever it is that you're setting that value, you also have to tell jQuery to trigger it.
HTML
JAVASCRIPT
should work as expected.
http://jsfiddle.net/7CM6k/3/
基于 Viktar 的答案,这里有一个实现,您可以在给定的隐藏输入元素上调用一次,以确保后续更改事件得到每当输入元素的值发生变化时触发:
在文档准备好时调用此方法,如下所示:
Building off of Viktar's answer, here's an implementation you can call once on a given hidden input element to ensure that subsequent change events get fired whenever the value of the input element changes:
Call this on document ready like so:
可以使用
Object.defineProperty()
来重新定义输入元素的“value”属性并在其更改期间执行任何操作。Object.defineProperty()
允许我们为属性定义 getter 和 setter,从而控制它。https://jsfiddle.net/bvvmhvfk/
It is possible to use
Object.defineProperty()
in order to redefine the 'value' property of the input element and do anything during its changing.Object.defineProperty()
allows us to define a getter and setter for a property, thus controlling it.https://jsfiddle.net/bvvmhvfk/
我从 Vikars 答案开始,但也注意到,当在 HTML 表单中使用隐藏表单字段时,最后的更改并没有'没有被提交。后来我找到了 Thomas 答案,所以我将两者结合到以下解决方案中,这似乎也适用于我的隐藏表单字段提交:
I started with Vikars answer, but noticed too that when the hidden form fields used in a HTML form that the last changes didn't get submited. Later on I found Thomas answer, so I combined both to the following solution, which seems to work nicely for my hidden form fields also on submit:
此示例在每次隐藏草稿字段更改其值时返回草稿字段值(chrome 浏览器):
This example returns the draft field value every time the hidden draft field changes its value (chrome browser):
虽然这个帖子已经有 3 年历史了,但我的解决方案如下:
Although this thread is 3 years old, here is my solution: