获取一个引用到Ajax.ActionLink的锚元素在onSuccess处理 [英] Get a reference to the anchor element of an Ajax.ActionLink at the OnSuccess handler
问题描述
基本上我的问题是相似或<一甚至重复href="http://stackoverflow.com/questions/1012599/asp-net-mvc-get-reference-to-triggerelement-in-onsuccess-function">this之一,但我使用MVC的剃刀。而且我敢肯定的答案的是过时的,因为目前使用的客户端库是jQuery的/不显眼的阿贾克斯。
所以,总结一下这个问题,我想访问触发在所提供的<的的onSuccess
属性指定的处理Ajax请求的锚元素code> AjaxOptions 。
下面是的ActionLink
:
@ Ajax.ActionLink(加入开放时间条目,AddOpeningTimes
新的HTML {preFIX中将Html.HTML = prefixFor(M =&GT; Model.OpeningTimes)},
新AjaxOptions {UpdateTargetId =openingTimes
InsertionMode = nsertionMode.InsertAfter,
的onSuccess =updateHtml preFIX},
新{标题=加入开放时间进入})
记者:
函数updateHtml preFIX(){
this.href = this.href.replace(?/ \ D(=] $)/,功能(我){返回++我;});
}
下面是一个链接到的答案显示了几个解决方案和问题的一个很好的标志说明。
http://stackoverflow.com/a/1068946/1563373
你总是可以只写
OnBegin =功能(){clickedLink = $(本);}
您可以再访问clickedLink变量中签处理(记得要与页面范围内声明它)。
编辑:
在一些与调用栈打转转,你可以尝试这样的:
&LT;脚本类型=文/ JavaScript的&GT;
功能启动(XHR){
VAR堆栈= start.caller;
//走栈
做 {
堆栈= stack.caller;
}而(stack.arguments =未定义&安培;!&安培; stack.arguments.length大于0&安培;&安培;!(stack.arguments [0] == .tagName未定义|| stack.arguments [0] .tagName =一个))
//堆栈现在指向切入点unobtrusive.ajax
如果(stack.arguments!=未定义)
xhr.originalElement = $(stack.arguments [0]);
// BLECH
}
功能UpdateHrefText(结果,状态XHR){
调试器;
如果(xhr.originalElement!=未定义)
xhr.originalElement.text(result.Message);
}
&LT; / SCRIPT&GT;
@ Ajax.ActionLink(测试,消息,家,新AjaxOptions {OnBegin =开始,的onSuccess =UpdateHrefText})
不知道我会虽然相信这个生产。我会做更多的东西一样:
&LT;脚本类型=文/ JavaScript的&GT;
VAR theLink;
功能启动(XHR){
xhr.originalElement = theLink;
}
功能UpdateHrefText(结果,状态XHR){
调试器;
如果(xhr.originalElement!=未定义)
xhr.originalElement.text(result.Message);
}
&LT; / SCRIPT&GT;
@ Ajax.ActionLink(测试,消息,家,空,新AjaxOptions {OnBegin =开始,的onSuccess =UpdateHrefText},{新的onclick =theLink = $(本);})
Basically my question is similar or even a duplicate of this one, except that I'm using MVC Razor. And I'm certain that the answers there are outdated since the client library currently used is jQuery / unobtrusive ajax.
So to sum up the question, I'm trying to access the anchor element that triggered the Ajax request in the handler specified at the OnSuccess
property of the provided AjaxOptions
.
Here is the ActionLink
:
@Ajax.ActionLink("Add opening times entry", "AddOpeningTimes",
new { htmlPrefix = Html.HtmlPrefixFor(m => Model.OpeningTimes) },
new AjaxOptions { UpdateTargetId = "openingTimes",
InsertionMode = nsertionMode.InsertAfter,
OnSuccess = "updateHtmlPrefix" },
new { title = "Add opening times entry" })
JS:
function updateHtmlPrefix() {
this.href = this.href.replace(/\d(?=]$)/, function (i) { return ++i; });
}
here is a link to an answer that shows several solutions and a good mark explanation of the issue.
http://stackoverflow.com/a/1068946/1563373
you could always just write
OnBegin="function() { clickedLink = $(this); }"
You can then access the clickedLink variable in the success handler (remember to declare it with page scope).
EDIT:
After some playing around with the call stack, you could try something like this:
<script type="text/javascript">
function start(xhr) {
var stack = start.caller;
// walk the stack
do {
stack = stack.caller;
} while (stack.arguments != undefined && stack.arguments.length > 0 && (stack.arguments[0].tagName == undefined || stack.arguments[0].tagName != "A"))
//stack now points to the entry point into unobtrusive.ajax
if (stack.arguments != undefined)
xhr.originalElement = $(stack.arguments[0]);
//blech
}
function UpdateHrefText(result, status, xhr) {
debugger;
if(xhr.originalElement != undefined)
xhr.originalElement.text(result.Message);
}
</script>
@Ajax.ActionLink("Test", "Message", "Home", new AjaxOptions{ OnBegin = "start", OnSuccess = "UpdateHrefText"})
Not sure I would trust this in production though. I'd do something more like:
<script type="text/javascript">
var theLink;
function start(xhr) {
xhr.originalElement = theLink;
}
function UpdateHrefText(result, status, xhr) {
debugger;
if(xhr.originalElement != undefined)
xhr.originalElement.text(result.Message);
}
</script>
@Ajax.ActionLink("Test", "Message", "Home", null, new AjaxOptions{ OnBegin = "start", OnSuccess = "UpdateHrefText"}, new { onclick="theLink = $(this);"})
这篇关于获取一个引用到Ajax.ActionLink的锚元素在onSuccess处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!