有没有办法让HTML select元素每次以编程方式更改其选择时调用一个函数?
当使用javascript修改选择框中的当前选择时,IE和FF都不会触发’onchange’.此外,js函数改变了选择是框架的一部分,所以我无法改变它以触发onchange(),例如.
这是一个例子:
n1
var inc = 1;
var sel = document.getElementById('sel1');
function test() {
inc++;
var o = new Option('n'+inc, inc);
sel.options[sel.options.length] = o;
o.selected = true;
sel.selectedIndex = sel.options.length - 1;
}
function myfunction() {
document.title += '[CHANGED]';
}
有没有办法让test()调用myfunction()而不更改test()(或在按钮上添加事件)?
谢谢.
解决方法:
如果您可以扩展/修改框架以在更改选择选项时提供钩子/回调,那么它会更好(一种方法是使用js的动态功能来将其输入?).
如果做不到这一点,就会出现效率低下的问题 – 民意调查.您可以设置一个setTimeout / setInteval调用来轮询所需的select选项dom元素,并在检测到某些内容发生更改时触发您自己的回调.
至于你的问题的答案
Is there any way to make test() call
myfunction() without changing test()
(or adding an event on the button)?
n1
var inc = 1;
var sel = document.getElementById('sel1');
function test() {
inc++;
var o = new Option('n'+inc, inc);
sel.options[sel.options.length] = o;
o.selected = true;
sel.selectedIndex = sel.options.length - 1;
}
function myfunction() {
document.title += '[CHANGED]';
}
//change to aop.after if you want to call afterwards
jQuery.aop.before( {target: window, method: 'test'},
function() {
myfunctino();
}
);
标签:javascript,events,html-select,html
来源: https://codeday.me/bug/20190522/1151731.html