隐藏输入更改事件 [英] hidden input change event

查看:17
本文介绍了隐藏输入更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测隐藏输入值的变化?我已经尝试过这些方法但没有成功:

How can I detect a change of the value of a hidden input? I've already tried these approaches without success:

$('#id_inpout').live('change',function () {
        var id_el = $(this).attr('id');
        alert(id_el);
    });

$('#id_inpout').change(function () {
        var id_el = $(this).attr('id');
        alert(id_el);
    });

$('#id_inpout').bind('change',function () {
        var id_el = $(this).attr('id');
        alert(id_el);
    });

推荐答案

正如在重复中所说的,正如你所看到的,在 javascript 中所做的更改不会触发 change 事件.

As was said in duplicates, and as you saw, changes done in javascript don't trigger the change event.

因为我没有在重复项中找到可接受的答案(即答案不涉及隐藏值设置方式的变化),并且假设您确实需要(也就是说您可以更改隐藏输入 val 时不要调用您的函数),您可以这样做:

As I didn't find acceptable answers in the duplicates (that is answers not involving a change in the way the hidden value is set), and supposing you really need that (that is you can't call your function when you change the hidden input val), here's what you might do :

function survey(selector, callback) {
   var input = $(selector);
   var oldvalue = input.val();
   setInterval(function(){
      if (input.val()!=oldvalue){
          oldvalue = input.val();
          callback();
      }
   }, 100);
}
survey('#id_inpout', function(){console.log('changed')}); 

但我最好使用更直接的解决方案(即在更改隐藏输入值时调用函数).这个函数可能是你自己的一个简单的事件调度器(基本上是一个对象,它包装了一个在调用时要调用的函数列表).

But I would preferably use a more direct solutions (i.e. call a function when you change the hidden input value). This function might be a simple event dispatcher of your own (basically an object wrapping a list of functions to call when it's called).

现在是 2015 年,人们想知道,不,网络世界的最新进展都没有解决这个问题.Mutation 观察者不观察输入的 value 属性(它不是真正的 DOM)并且 ES6 对象观察者对这些原生对象也无能为力.

It's now 2015 and for people wondering, no, none of the recent advances of the web world solve that problem. Mutation observers don't observe the value property of the input (it's not really the DOM) and ES6 object observer are powerless for those native objects too.

这篇关于隐藏输入更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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