ASP.NET MVC Ajax - É possível executar um link -GENERATED- Ajax com entrada de teclado?
-
05-09-2019 - |
Pergunta
Tem alguém capaz de -execute- um Ajax.ActionLink gerado quando um usuário pressiona uma tecla no teclado? (Isso é necessário para acessibilidade)
NOTA: Eu estou usando bibliotecas ASP.NET MVC + Microsoft Js (... Ajax.js / ... MvcAjax.js) + jQuery
Javascript para keypress captura (IE + Firefox)
$(document).keypress(function(event) {
if(event.keyCode == 27) {
//execution here
var a = document.getElementById('linkid');
}
});
HTML gerado pelo 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>
O seguinte não é o que eu estou procurando, isso não funciona!
$(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
}
});
Solução
Você já tentou usar mecanismo de gatilho do jQuery?
$(document).keypress( function(e) {
if (e.keyCode == 27) {
$(this).trigger('click');
}
}
Se isso falhar, você pode simplesmente invocar a href, que vai fazer um postback completo, mas deve realizar a ação desejada se a ação é escrito para lidar com solicitações tanto AJAX e não-Ajax.
$(document).keypress( function(e) {
if (e.keyCode == 27) {
location.href = $(this).attr('href');
}
}
Outras dicas
Por favor Tente isto:
var a = document.getElementById('linkid');
$(a).trigger('click');
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow