小编Mic*_*ake的帖子

如何在MVC3 Ajax.ActionLink OnBegin,OnComplete Events中使用$(this)

我的控制器创建了这样的链接列表

<ul id="MainMenu">
@foreach (var item in Model.MenuItems)
{
    <li>@Ajax.ActionLink(item.Caption,
    item.ActionName,
    item.ControllerName,
    new AjaxOptions
    {
        UpdateTargetId = "pageBody",
        OnBegin = "BeginUpdatePage",
        OnComplete = "EndUpdatePage",
        OnFailure = "FailUpdatePage"
    })
    </li>
}
</ul>
Run Code Online (Sandbox Code Playgroud)

我有一些像这样的JavaScript

function BeginUpdatePage() {
 $("#MainMenu li").removeClass('selected');
 $(this).parent().addClass('selected');
 $("#pageBody").fadeTo('slow', 0.5);
}

function EndUpdatePage() {
 $("#pageBody").fadeTo('slow', 1);    
}

function FailUpdatePage() {
 alert('There has been an error loading this page please try again.');
}
Run Code Online (Sandbox Code Playgroud)

在BeginUpdatePage函数第1行和第3行执行正常,但第2行"$(this).parent().addClass('selected');" 并没有明显地做任何事情......(我已经在我的css中宣布了这个课程).

我查看了Jquery文档(Jquery.ajax()),它说"this"是被点击的元素.我还检查了默认情况下在我的项目中的"jquery.unobtrusive-ajax.js".执行我的函数时,此文件也会传递"this".

"result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(this,arguments);"
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么......我见过其他使用过这种技术的例子但是我整天都失败了!

如何在Begin,complete函数中找到被单击的元素.

jquery asp.net-mvc-3 unobtrusive-ajax

10
推荐指数
2
解决办法
9539
查看次数

标签 统计

asp.net-mvc-3 ×1

jquery ×1

unobtrusive-ajax ×1