jQuery - 检测隐藏输入字段的值变化 [英] jQuery - Detect value change on hidden input field

查看:29
本文介绍了jQuery - 检测隐藏输入字段的值变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏的文本字段,其值通过 AJAX 响应更新.

I have a hidden text field whose value gets updated via an AJAX response.

<input type="hidden" value="" name="userid" id="useid" />

当这个值改变时,我想触发一个 AJAX 请求.谁能建议如何检测变化?

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!'); 
}) 

推荐答案

所以这已经很晚了,但我已经找到了一个答案,以防万一它对遇到此线程的任何人有用.

So this is way late, but I've discovered an answer, in case it becomes useful to anyone who comes across this thread.

对隐藏元素的值更改不会自动触发 .change() 事件.因此,无论您在哪里设置该值,您还必须告诉 jQuery 触发它.

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.

function setUserID(myValue) {
     $('#userid').val(myValue)
                 .trigger('change');
}

既然如此,

$('#userid').change(function(){
      //fire your ajax call  
})

应该按预期工作.

这篇关于jQuery - 检测隐藏输入字段的值变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆