jQuery onchange检测 [英] jQuery onchange detection
问题描述
我想执行html元素的onchange属性中的javascript。所以..
I want to execute javascript that's in the "onchange" attribute of an html element. So..
<input id="el" type="text" onchange="alert('test');" value="" />
使用该示例我想执行al ert('test ');
部分通过jQuery,问题是 .change()
事件处理程序无法正常工作,因为我想在另一个之后执行它元素改变了它的价值。那么......
Using that example I'd like to execute the alert('test');
portion via jQuery, the problem is the .change()
event handler isn't working, because I want to execute it after another element changes it's value. So..
$('#el').val('test');
这是我想要执行onchange的时候。之后, .val
已被调用。有什么想法?
This is when I'd like to execute the onchange. After that .val
val has been called. Any ideas?
推荐答案
.val()
不会触发更改
事件。你需要自己手动完成。例如:
.val()
does not trigger change
events. You need to do so yourself, manually. For example:
$('#el').val('test').change();
你可以将它封装在一个小的jQuery插件中,如下所示:
You can encapsulate this in a small jQuery plugin like so:
(function ($) {
$.fn.changeVal = function (text) {
this.val(text).change();
};
}(jQuery));
$('#el').changeVal('test');
这篇关于jQuery onchange检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!