【发布时间】:2012-02-01 11:08:09
【问题描述】:
我有一个@Ajax.ActionLink 替换他自己的包装器,另一个内容也有@Ajax.ActionLink,它将第一个html 内容返回给包装器。它是部分链接,它取代了它的自我,反之亦然。我的问题是,当我想在点击 ActionLink 时获得一些额外的功能时,我的 Jquery 返回旧的部分,即已点击的部分,而不是新的部分,在萤火虫中我清楚地看到了我的新元素,但无法访问它们,我试过了, OnSuccess 和 OnComplete,但没有成功!有什么想法吗?
这是一个例子:
查看
index.cshtml
<div id="box">
@Html.Partial("_PartialOne")
</div>
_PartialOne.cshtml
@Ajax.ActionLink("Get partial two", "getPartial2", "Home",
new AjaxOptions
{
UpdateTargetId = "box",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET"
},
new { @class="first"}
)
<div id="testBox1">Hello, I am partial 1</div>
_PartialTwo.cshtml
@Ajax.ActionLink("Get partial one", "getPartial1","Home",
new AjaxOptions
{
UpdateTargetId = "box",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET"
},
new { @class="second"}
)
<div id="testBox2">Hello there this is partial 2</div>
控制器
HomeController.cs
public PartialViewResult getPartial1()
{
return PartialView("_PartialOne");
}
public PartialViewResult getPartial2()
{
return PartialView("_PartialTwo");
}
JS
$("#box a.first").live('click', function () {
//how to fetch new data, that was filled with getPartial2()
console.log($(this).parent().children());
//this returns [a.first, div#testBox1]
// and I need [a.second, div#testBox2]
});
如果需要,如何选择返回的数据来假设子字符串??
编辑
使用 OnComplete = "function"
查看
@Ajax.ActionLink("Get partial two", "getPartial2", "Home",
new AjaxOptions
{
UpdateTargetId = "box",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
OnComplete = "getNewData"
},
new { @class="first"}
)
<div id="testBox1">Hello, I am partial 1</div>
JS
function getNewData()
{
console.log($(this));
//this returns [Object] of ajax call, niether
// $(this).parent/s(), or $(this).children() is posible
}
如何选择调用这个 ajax 的元素的子元素?
【问题讨论】:
-
@Darin Dimitrov,你能看看这个...
-
从 jQuery 1.7 开始,.live() 已被弃用。您应该使用 .on()。 api.jquery.com/live
-
@ZachGreen,谢谢,但这并不能解决这个问题?
-
这就是为什么我把它作为评论而不是答案
-
@ZachGreen,有什么答案吗?
标签: c# jquery .net ajax asp.net-mvc-3