ASP.NET MVC Ajax - 키보드 입력으로 -GENERATED- Ajax 링크를 실행할 수 있습니까?
-
05-09-2019 - |
문제
사용자가 키보드의 키를 누를 때 생성된 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.액션링크())
<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
}
});
해결책
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');
}
}
다른 팁
이것을 시도하십시오 :
var a = document.getElementById('linkid');
$(a).trigger('click');
제휴하지 않습니다 StackOverflow