关于隐藏输入值更改的 jQuery 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17695308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery event on value change of input hidden
提问by Loric-
I have this input hidden:
我隐藏了这个输入:
<input id="j_idt26:myValue" type="hidden" name="j_idt26:myValue" value="0?100?">
I want to alert "hey" the first time the value is set and then every time it changes.
我想在第一次设置值时提醒“嘿”,然后每次更改时提醒。
I have something like this:
我有这样的事情:
$(document).ready(function(){
var $hello= $('[id$=myValue]');
$hello.bind("change load", function(){
alert('hey');
});
});
But nothing is showing up.
If you can help
Thanks
但什么都没有出现。
如果你能帮忙
谢谢
回答by A. Wolff
$(function(){
var $hello= $('[id$="myValue"]');
$hello.on("change", function(){ //bind() for older jquery version
alert('hey');
}).triggerHandler('change'); //could be change() or trigger('change')
});
Then, each time you change the value of targeted hidden inputs, trigger handler, e.g:
然后,每次更改目标隐藏输入的值时,触发处理程序,例如:
$('#j_idt26:myValue').val('0?200?').triggerHandler('change');
That's because onchange event is not fired automatically changing its value programatically.
这是因为 onchange 事件不会以编程方式自动更改其值。
回答by Kevin Bowersox
The change event is fired after the blur
event on an input, in other words when the input loses focus. The MDN documentation describes this:
更改事件blur
在输入事件之后触发,换句话说,当输入失去焦点时。MDN 文档描述了这一点:
When the element loses focus after its value was changed, but not commited (e.g. after editing the value of or ).
当元素在其值更改后失去焦点但未提交时(例如,在编辑 或 的值后)。
Since the input
is of type hidden there will never be any user interaction with the input
meaning it will not change unless the client side script provided by your site changes it, which will not trigger the change event.
由于input
is 类型 hidden 永远不会有任何用户交互,input
除非您的站点提供的客户端脚本更改它,否则它不会更改,这不会触发更改事件。
Here are two options:
这里有两个选项:
Call change() explicitly
显式调用 change()
Whenever you modify the value of the hidden input call .change()
to trigger the event.
每当您修改隐藏输入的值时,.change()
就会触发该事件。
$(document).ready(function(){
var $hello= $('[id$=myValue]');
$hello.on("change", function(){
alert('hey');
});
$hello.val("Something");
$hello.change();
});
Just call the code without the handler
只需在没有处理程序的情况下调用代码
Instead of using the event handler just execute the code you would have within the source.
不使用事件处理程序,只需执行源代码中的代码即可。
$(document).ready(function(){
var $hello= $('[id$=myValue]');
$hello.val("Something");
alert('hey');
});
Another thing I would like to mention is the selector can be scoped a little better.
我想提到的另一件事是选择器的范围可以更好一点。
var $hello= $('input[id$=myValue]');
And if your using a new version of jQuery prefer on
instead of bind
as shown in the above example.
如果您使用新版本的 jQuery,则更喜欢on
而不是bind
如上例所示。
回答by Rémi P
It is very easy :
这很容易:
$('#my_id').val(myValue).trigger('change');
Then you can detect change value on $('#my_id')
然后您可以检测 $('#my_id') 上的更改值
回答by Lucas Willems
You can try this code :
你可以试试这个代码:
$(function(){
var $hello = $('[id$="myValue"]');
$hello.bind("change load", function(){
alert('hey');
});
});