ASP.NET MVC Ajax - E 'possibile eseguire un collegamento -GENERATED- Ajax con input da tastiera?
-
05-09-2019 - |
Domanda
C'è qualcuno in grado di -execute- un Ajax.ActionLink generato quando un utente preme un tasto sulla tastiera? (Questo è necessario per l'accessibilità)
Nota: sto usando le librerie Microsoft ASP.NET MVC + js (... Ajax.js / ... MvcAjax.js) + jQuery
Javascript per catturare pressione del tasto (IE + Firefox)
$(document).keypress(function(event) {
if(event.keyCode == 27) {
//execution here
var a = document.getElementById('linkid');
}
});
HTML generato da 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>
Il seguente non è quello che sto cercando, questo non funziona!
$(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
}
});
Soluzione
Hai provato a usare il meccanismo di innesco di jQuery?
$(document).keypress( function(e) {
if (e.keyCode == 27) {
$(this).trigger('click');
}
}
In mancanza di questo, si può solo invocare il href, che farà un postback completo, ma dovrebbe compiere l'azione desiderata se l'azione viene scritto per gestire sia AJAX e le richieste non-AJAX.
$(document).keypress( function(e) {
if (e.keyCode == 27) {
location.href = $(this).attr('href');
}
}
Altri suggerimenti
Si prega di provare questo:
var a = document.getElementById('linkid');
$(a).trigger('click');