gpt4 book ai didi

ajax - 在 OnSuccess 处理程序中获取对 Ajax.ActionLink 的 anchor 元素的引用

转载 作者:行者123 更新时间:2023-12-01 06:39:36 24 4
gpt4 key购买 nike

基本上我的问题与this 相似甚至重复一,除了我使用的是 MVC Razor。而且我确信那里的答案已经过时,因为当前使用的客户端库是 jQuery/unobtrusive ajax。

所以总结一下这个问题,我试图在提供的 AjaxOptionsOnSuccess 属性指定的处理程序中访问触发 Ajax 请求的 anchor 元素.

这是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; });
}

最佳答案

这里是一个答案的链接,其中显示了多个解决方案以及对问题的良好解释。

https://stackoverflow.com/a/1068946/1563373

你总是可以写

 OnBegin="function() { clickedLink = $(this); }" 

然后您可以在成功处理程序中访问 c​​lickedLink 变量(记得用页面范围声明它)。

编辑:

在尝试调用堆栈之后,您可以尝试这样的操作:

<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"})

虽然不确定我是否会在生产中相信这一点。我会做更多类似的事情:

<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 - 在 OnSuccess 处理程序中获取对 Ajax.ActionLink 的 anchor 元素的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16454381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com