IE9 的 AppendText 和 TypeText 问题(特别是更改事件)

发布于 2024-11-30 08:52:50 字数 3050 浏览 0 评论 0原文

因此,最近我们强制我们的应用程序在 IE9 模式下运行,从那时起,WatiN typetext、AppendText 与 TriggerClientChange(如下所示)结合使用不再触发页面上的 JS。

情况是这样的...它是一个文本字段,用于快速搜索我们的应用程序。 OnFocus 会弹出一个菜单,当输入时,它会搜索数据库并用结果填充选择菜单。在过去的两年里,使用 AppendText 和 TypeText 以及此方法 TriggerClientChange 一直运行良好。

    public static void TriggerClientChange(this Element element)
    {
        //var Id = element.Id.Replace("[", @"\\[").Replace("]", @"\\]");
        if (String.IsNullOrEmpty(element.Id))
        {
            element.Id = "x" + Guid.NewGuid().ToString("N");
        }

        string selector = "[id=\"" + element.Id + "\"]";

        //if (element.Name == null)
        //    throw new ElementExtensionException("element: " + element.ToString() + " doesn't exist.");

        if (element is CheckBox)
        {
            selector += ":checkbox";
        }
        element.DomContainer.WaitForComplete();
        element.DomContainer.Eval("var elt = window.$('" + selector + "'); if(elt.length > 0) { elt.trigger('change'); }");
        element.DomContainer.WaitForAjaxComplete();
        element.DomContainer.WaitForComplete();
    }

现在我们支持 IE9 和 HTML5,我们决定强制使用 IE9 和标准。从那时起,当在搜索文本字段中键入值时,调用运行 JS 的 TriggerClientChange 时,不会触发 JS。我们尝试了 .change 的不同排列,但没有成功,例如:

    public static void ForceChange(this Element element)
    {
        element.DomContainer.Eval(String.Format("$({0}).change();", element.GetJavascriptElementReference()));
    }
    public static void ForceChange(this Element element)
    {
        element.DomContainer.Eval("$('#" + element.Id + "').change();");
    }

都没有像以前那样产生结果。下面是在文本框中输入值的快速搜索方法,该方法已经工作了很长时间,直到我们强制执行 IE9 标准

    internal void SetQuickSearchTextbox(string val)
    {
        this.Browser.TextField("navBarQuickContactSearch").AppendText(val);
        this.Browser.TextField("navBarQuickContactSearch").TriggerClientChange();
        this.Browser.WaitForAjaxComplete();

        Thread.Sleep(5000);

        ElementCollection results = this.Browser.Div(Find.ByClass("ac_results")).ElementsWithTag("li");
        results[1].Click();
    }

。最后这是 TypeText

    protected virtual void SendKeyPresses(string value)
    {
        var length = value != null ? value.Length : 0;

        if (TextField.MaxLength != 0 && length > TextField.MaxLength)
        {
            length = TextField.MaxLength;
        }

        for (var i = 0; i < length; i++)
        {
            var character = value[i];

            FireKeyDown(character);
            FireKeyPress(character);
            FireKeyUp(character);
        }
    }

    protected virtual void FireKeyUp(char character)
    {
        TextField.KeyUp(character);
    }

    protected virtual void FireKeyPress(char character)
    {
        TextField.KeyPress(character);
    }

    protected virtual void FireKeyDown(char character)
    {
        TextField.KeyDown(character);
    }

总结:这些方法一直有效,直到强制执行 IE9 标准。是的,这在手动输入文本字段时有效,但现在脚本不会像以前那样更改元素。任何帮助都会很棒。

So recently we have forced our app to run in IE9 mode, and since then WatiN typetext, AppendText, with conjuntion with TriggerClientChange (displayed below) does not fire the JS on the page anymore.

Here is the situation... Its a textfield that is used for quick searching our app. OnFocus, pops up a menu, and when typing occurs, it then searches the DB and populates teh select menu with results. for the past two years, this has been working fine using AppendText and TypeText with this Method TriggerClientChange

    public static void TriggerClientChange(this Element element)
    {
        //var Id = element.Id.Replace("[", @"\\[").Replace("]", @"\\]");
        if (String.IsNullOrEmpty(element.Id))
        {
            element.Id = "x" + Guid.NewGuid().ToString("N");
        }

        string selector = "[id=\"" + element.Id + "\"]";

        //if (element.Name == null)
        //    throw new ElementExtensionException("element: " + element.ToString() + " doesn't exist.");

        if (element is CheckBox)
        {
            selector += ":checkbox";
        }
        element.DomContainer.WaitForComplete();
        element.DomContainer.Eval("var elt = window.$('" + selector + "'); if(elt.length > 0) { elt.trigger('change'); }");
        element.DomContainer.WaitForAjaxComplete();
        element.DomContainer.WaitForComplete();
    }

Now that we are supporting IE9, and HTML5 we have decided to force IE9, and standards. Since then, the JS is not being fired when TriggerClientChange is envoked, which runs the JS, when a value is typed in the search textfield. We have tried different permutations of the .change without success, shuch as:

    public static void ForceChange(this Element element)
    {
        element.DomContainer.Eval(String.Format("$({0}).change();", element.GetJavascriptElementReference()));
    }
    public static void ForceChange(this Element element)
    {
        element.DomContainer.Eval("$('#" + element.Id + "').change();");
    }

Neither produce the result, as it used to. Below is the QuickSearch Method that types the value into textbox that has worked for a long time, until we forced IE9 standards

    internal void SetQuickSearchTextbox(string val)
    {
        this.Browser.TextField("navBarQuickContactSearch").AppendText(val);
        this.Browser.TextField("navBarQuickContactSearch").TriggerClientChange();
        this.Browser.WaitForAjaxComplete();

        Thread.Sleep(5000);

        ElementCollection results = this.Browser.Div(Find.ByClass("ac_results")).ElementsWithTag("li");
        results[1].Click();
    }

Lastly this is the TypeText

    protected virtual void SendKeyPresses(string value)
    {
        var length = value != null ? value.Length : 0;

        if (TextField.MaxLength != 0 && length > TextField.MaxLength)
        {
            length = TextField.MaxLength;
        }

        for (var i = 0; i < length; i++)
        {
            var character = value[i];

            FireKeyDown(character);
            FireKeyPress(character);
            FireKeyUp(character);
        }
    }

    protected virtual void FireKeyUp(char character)
    {
        TextField.KeyUp(character);
    }

    protected virtual void FireKeyPress(char character)
    {
        TextField.KeyPress(character);
    }

    protected virtual void FireKeyDown(char character)
    {
        TextField.KeyDown(character);
    }

Summary: These have worked until IE9 was forced. Yes this works when manually typing in the textfield, yet now the scripts do not .change the element like it used to. Any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-12-07 08:52:50

我们使用以下代码片段修复了该问题。它模拟缺失的 jquery 事件:

var evt = jQuery.Event("keydown");   
evt.keyCode = $.ui.keyCode.RIGHT;   
$('input.ac_input').focus().val('SOME TEST TEXT').trigger(evt);

这是通过 Browser.Eval 运行的解决方法。 ie9mode 的改变破坏了它仍然没有确定。

We fixed the issue with the following snippet. It simulates the missing jquery event:

var evt = jQuery.Event("keydown");   
evt.keyCode = $.ui.keyCode.RIGHT;   
$('input.ac_input').focus().val('SOME TEST TEXT').trigger(evt);

It's a workaround that is run through Browser.Eval. The change in ie9mode that broke it is still not determined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文