问题描述
如何检测隐藏输入值的变化?我已经尝试过这些方法而没有成功:
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年,对于人们想知道的是,网络世界的最新进展都无法解决该问题.突变观察者不会观察到输入的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.
这篇关于隐藏的输入更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!