ASP.NET MVC Ajax - 是否可以使用键盘输入执行 -GENERATED- Ajax 链接?

发布于 2024-07-21 05:36:14 字数 1388 浏览 8 评论 0原文

当用户按下键盘上的某个键时,是否有人能够执行生成的 Ajax.ActionLink?(这是辅助功能所必需的)

注意:我正在使用 ASP。 NET MVC + Microsoft Js 库 (...Ajax.js / ...MvcAjax.js) + jQuery

Javascript 捕获按键 (IE + Firefox)

$(document).keypress(function(event) {
    if(event.keyCode == 27) {
        //execution here
        var a = document.getElementById('linkid');
    }
});

由 ASP.NET MVC 生成的 Html (Ajax.ActionLink())

<a id="linkid" href="/controller/action" onclick="
Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), 
{ insertionMode: Sys.Mvc.InsertionMode.replace, 
updateTargetId: 'SomeDivId' });
">LinkText</a>

以下不是我要找的内容,这不起作用!

$(document).keypress(function(event) {
    if(event.keyCode == 27) {
        var a = document.getElementById('linkid');
        a.onclick();           //doesn't exist in Firefox
        a.click();             //doesn't "work" in Firefox (reference to "this" [a] is needed .NET MVC javascript)
        a["onclick"]();        //same as .onclick()
        a["click"]();          //same as .click()

        //or even:
        a.onclick.apply(a);    //doesn't exist in Firefox
        a.click.apply(a);      //Somehow keeps "this" reference, but throws Sys.ArgumentUndefinedException
    }
});    

Is anybody able to -execute- a generated Ajax.ActionLink when a user presses a key on the keyboard? (This is needed for accessibility)

NOTE: I'm using ASP.NET MVC + Microsoft Js libraries (...Ajax.js / ...MvcAjax.js) + jQuery

Javascript to capture keypress (IE + Firefox)

$(document).keypress(function(event) {
    if(event.keyCode == 27) {
        //execution here
        var a = document.getElementById('linkid');
    }
});

Html generated by ASP.NET MVC (Ajax.ActionLink())

<a id="linkid" href="/controller/action" onclick="
Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), 
{ insertionMode: Sys.Mvc.InsertionMode.replace, 
updateTargetId: 'SomeDivId' });
">LinkText</a>

The following is not what i'm looking for, this doesn't work!

$(document).keypress(function(event) {
    if(event.keyCode == 27) {
        var a = document.getElementById('linkid');
        a.onclick();           //doesn't exist in Firefox
        a.click();             //doesn't "work" in Firefox (reference to "this" [a] is needed .NET MVC javascript)
        a["onclick"]();        //same as .onclick()
        a["click"]();          //same as .click()

        //or even:
        a.onclick.apply(a);    //doesn't exist in Firefox
        a.click.apply(a);      //Somehow keeps "this" reference, but throws Sys.ArgumentUndefinedException
    }
});    

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

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

发布评论

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

评论(2

悲凉≈ 2024-07-28 05:36:14

您尝试过使用 jQuery 的触发机制吗?

$(document).keypress( function(e) {
    if (e.keyCode == 27) {
       $(this).trigger('click');
    }
}

如果做不到这一点,您可以只调用 href,这将执行完整的回发,但如果该操作被编写为处理 AJAX 和非 AJAX 请求,则应该完成所需的操作。

$(document).keypress( function(e) {
    if (e.keyCode == 27) {
       location.href = $(this).attr('href');
    }
}

Have you tried using jQuery's trigger mechanism?

$(document).keypress( function(e) {
    if (e.keyCode == 27) {
       $(this).trigger('click');
    }
}

Failing that, you could just invoke the href, which will do a full postback, but should accomplish the desired action if the action is written to handle both AJAX and non-AJAX requests.

$(document).keypress( function(e) {
    if (e.keyCode == 27) {
       location.href = $(this).attr('href');
    }
}
飘落散花 2024-07-28 05:36:14

请尝试这个:

            var a = document.getElementById('linkid');
        $(a).trigger('click');

Please Try this:

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