简体   繁体   English

Javascript手动触发.onchange()事件

[英]Javascript manually firing .onchange() event

I have an input field I want to assign a new value and fire an .onchange() event. 我有一个输入字段,我想分配一个新值并触发一个.onchange()事件。 I did the following: 我做了以下事情:

document.getElementById("range").value='500';
document.getElementById("range").onchange();

Where range is my input Id. 范围是我的输入ID。 I get the following error: 我收到以下错误:

Uncaught TypeError: Cannot read property 'target' of undefined

Is there a way to define the 'target'? 有没有定义“目标”的方法? Thank you 谢谢

The error about target is because there's code in the event handler that's trying to read the target property of the Event object associated with the change event. 关于target的错误是因为事件处理程序中有代码试图读取与change事件关联的Event对象的target属性。 You could try passing in an faux-Event to fool it: 您可以尝试传递一个伪事件来欺骗它:

var range= document.getElementById('range');
range.onchange({target: range});

or, if you can, change the handler code to use this instead of event.target . 或者,如果可以的话,更改处理程序代码以使用this而不是event.target Unless you are using delegation (catching change events on child object from a parent, something that is troublesome for change events because IE doesn't 'bubble' them), the target of the change event is always going to be the element the event handler was registered on, making event.target redundant. 除非您使用委派(从父级在子对象上捕获更改事件,这对于更改事件很麻烦,因为IE不会“冒泡”它们),所以更改事件的目标始终是事件处理程序的元素已注册,从而使event.target变得多余。

If the event handler uses more properties of Event than just target you would need to fake more, or go for the 'real' browser interface to dispatching events. 如果事件处理程序使用Event属性多于target那么您将需要伪造更多,或者使用“真实的”浏览器界面来调度事件。 This will also be necessary if event listeners might be in use ( addEventListener , or attachEvent in IE) as they won't be visible on the direct onchange property. 如果可能正在使用事件侦听器(IE中的addEventListenerattachEvent ),这也将是必需的,因为它们不会在直接onchange属性上可见。 This is browser-dependent ( fireEvent for IE, dispatchEvent for standards) and not available on older or more obscure browsers. 这是与浏览器相关的(对于IE是fireEvent ,对于标准是dispatchEvent ),并且在较旧或更模糊的浏览器上不可用。

Try using fireEvent or dispatchEvent (depending on browser) to raise the event: 尝试使用fireEvent或dispatchEvent(取决于浏览器)引发事件:

document.getElementById("range").value='500';
if (document.getElementById("range").fireEvent) {
    document.getElementById("range").fireEvent("onclick");
} else if (document.getElementById("range").dispatchEvent) {
    var clickevent=document.createEvent("MouseEvents");
    clickevent.initEvent("click", true, true);
    document.getElementById("range").dispatchEvent(clickevent);
}

Generally, your code should work fine. 通常,您的代码应该可以正常工作。 There might be something else that's issuing the problem, though. 不过,可能还有其他原因在发出问题。

  • Where do you run those two lines? 您在哪里运行这两行?
  • Are you sure that the element with the range id is loaded by the time you run the code (eg you run it in document.ready). 您确定在运行代码时已加载具有range ID的元素(例如,在document.ready中运行它)。
  • Are you sure that you only have one element with id range on the page? 您确定页面上只有一个 ID range为ID的元素吗?
  • What is your onchange() function doing (could be helpful to post it here)? 您的onchange()函数在做什么(将其张贴在此处会有所帮助)?

Apart from that, I would recommend using jQuery (if possible): 除此之外,我建议使用jQuery(如果可能):

$('#range').trigger('change');

or just 要不就

$('#range').change();

http://api.jquery.com/change/ http://api.jquery.com/change/

But as I mentioned, your case should work fine too: http://jehiah.cz/a/firing-javascript-events-properly 但正如我所提到的,您的案例也应该可以正常工作: http : //jehiah.cz/a/firing-javascript-events-properly

from : http://www.mail-archive.com/jquery-en@googlegroups.com/msg44887.html 来自: http : //www.mail-archive.com/jquery-zh@googlegroups.com/msg44887.html

Sometimes it's needed to create an event programmatically. 有时需要以编程方式创建事件。 (Which is different from running an event function (triggering) (与运行事件函数(触发)不同

This can be done by the following fire code 可以通过以下防火代码来完成

> var el=document.getElementById("ID1")
> 
> fire(el,'change')
> 
> 
>    function fire(evttype) {
>        if (document.createEvent) {
>          var evt = document.createEvent('HTMLEvents');
>          evt.initEvent( evttype, false, false);
>          el.dispatchEvent(evt);
>        } else if (document.createEventObject) {
>          el.fireEvent('on' + evttype);
>        }    } looks like this trick is not yet in jQuery, perhaps for a
> reason?

This seems to work for me (see this fiddle ). 这似乎对我有用 (请参阅此小提琴 )。 Do you have any other code that may be the problem? 您还有其他可能是问题的代码吗? How did you define your onchange handler? 您如何定义onchange处理程序?

Are you calling e.target in your onchange handler? 您是在onchange处理程序中调用e.target吗? I suspect this may be the issue... since you are doing the change programmatically, there is no corresponding window event. 我怀疑这可能是问题所在...由于您以编程方式进行更改,因此没有相应的窗口事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM