编程触发在Javascript事件使用jQuery的IE浏览器 [英] Programmatically triggering events in Javascript for IE using jQuery

查看:132
本文介绍了编程触发在Javascript事件使用jQuery的IE浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个事件是通过在IE的用户触发,它被设置为 window.event 对象。看什么触发事件的唯一方法是通过访问 window.event 对象(据我所知)

When an Event is triggered by a user in IE, it is set to the window.event object. The only way to see what triggered the event is by accessing the window.event object (as far as I know)

这会导致如果编程触发事件,通过jQuery的触发事件时,就像在ASP.NET中的一个问题验证器。在这种情况下, window.event 对象存储的最后一个用户触发的事件。

This causes a problem in ASP.NET validators if an event is triggered programmatically, like when triggering an event through jQuery. In this case, the window.event object stores the last user-triggered event.

在该的onchange 事件是具有附加给它的ASP.NET验证的文本框编程发射,验证休息,因为它是在寻找引发该元素最后一个事件,这是不验证器是为元素

When the onchange event is fired programmatically for a text box that has an ASP.NET validator attached to it, the validation breaks because it is looking at the element that fired last event, which is not the element the validator is for.

有谁知道解决的办法?好像是可以解决的问题,但是从网上看,大多数人只是想办法忽视的问题,而不是解决它。

Does anyone know a way around this? It seems like a problem that is solvable, but from looking online, most people just find ways to ignore the problem instead of solving it.


为了解释我专门做什么:

我使用的是一个文本框,也有与之相关联的2 ASP.NET验证一个jQuery的时间选择器插件。当时间被改变,我使用的是更新面板回发到服务器,以动态地做一些事情,所以我需要onchange事件,以触发该文本框回发火灾。

To explain what I'm doing specifically:
I'm using a jQuery time picker plugin on a text box that also has 2 ASP.NET validators associated with it. When the time is changed, I'm using an update panel to post back to the server to do some things dynamically, so I need the onchange event to fire in order to trigger the postback for that text box.

jQuery的时间选择器创建单击文本框时变得可见一个隐藏的无序列表操作。当点击列表中的项目之一,变事件通过jQuery的改变文本框编程解雇()方法。

The jQuery time picker operates by creating a hidden unordered list that is made visible when the text box is clicked. When one of the list items is clicked, the "change" event is fired programmatically for the text box through jQuery's change() method.

由于该事件的触发是一个列表项,IE浏览器看到的项目的作为事件,而不是文本框的源像它应该。

Because the trigger for the event was a list item, IE sees the list item as the source of the event, not the text box, like it should.

我不是太在意这个ASP.NET验证只要文本框更改工作,我只需要在变更被如此处理事件我回发事件被称为文本框。问题在于,验证器抛出在IE异常这样就不会触发任何事件。

I'm not too concerned with this ASP.NET validator working as soon as the text box is changed, I just need the "change" event to be processed so my postback event is called for the text box. The problem is that the validator throws an exception in IE which stops any event from being triggered.

火狐(和我假设其他浏览器)没有这个问题。只有IE浏览器由于不同的事件模型。有没有人遇到过这一点,并看到了如何解决它?

Firefox (and I assume other browsers) don't have this issue. Only IE due to the different event model. Has anyone encountered this and seen how to fix it?


我发现这个问题报道的其他几个地方,但他们提供的解决方案没有:

I've found this problem reported several other places, but they offer no solutions:

  • jQuery's forum, with the jQuery UI Datepicker and an ASP.NET Validator
  • ASP.NET forums, bug with ValidatorOnChange() function

推荐答案

我有同样的问题。使用此功能解决:

I had the same problem. Solved by using this function:

jQuery.fn.extend({
    fire: function(evttype){ 
        el = this.get(0);
        if (document.createEvent) {
            var evt = document.createEvent('HTMLEvents'); 
            evt.initEvent(evttype, false, false); 
            el.dispatchEvent(evt); 
        } else if (document.createEventObject) { 
            el.fireEvent('on' + evttype); 
        }
        return this;
    }
});

所以,我的ONSELECT事件处理程序日期选择器看起来像:

So my "onSelect" event handler to datepicker looks like:

if ($.browser.msie) {
    datepickerOptions = $.extend(datepickerOptions, { 
        onSelect: function(){
            $(this).fire("change").blur();
        }
    });
}

这篇关于编程触发在Javascript事件使用jQuery的IE浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆