ASP.NETMVC-jQueryActionLink和Ajax在div单击时的调用
我正在使用ActionLink调用下面的方法ASP.NETMVC-jQueryActionLink和Ajax在div单击时的调用,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我正在使用ActionLink调用下面的方法 @Html.ActionLink("Add another...", "addLine",Nothing,New With {.id = "addItem"}) $("#addItem").click(function () { alert(this.href); $.ajax({ url: this.href, cache: false, su
@Html.ActionLink("Add another...", "addLine",Nothing,New With {.id = "addItem"})
$("#addItem").click(function () {
alert(this.href);
$.ajax({
url: this.href,
cache: false,
success: function (html) {
$("#editorrows").append(html);
}
});
return false;
});
它很好用。
我想在用户单击DIV时复制相同的行为。我尝试了一些方法,但它们似乎不起作用,我猜是因为链接调用了URL?它在DIV上不起作用,因为您的代码中有以下内容:
url: this.href
div可能没有href属性。
尝试在JavaScript代码中显式指定URL
url: '/controller/addLine?id="addItem"'
但是,您将失去基于路由的动态URL生成。
或者,将URL存储在div的属性中,并从JavaScript读取:
<div data-action-url="@Url.Action("addLine", Nothing, New With { .id = "addItem" })"></div>
url: $(this).data("action-url")
我不明白这个问题。如果它工作正常,有什么问题吗?当我尝试div ID时,它不起作用。正如我在原始帖子中所说,我认为这是因为操作链接在单击div时发出GET请求或其他东西不会这样做。显然,它不会与div一起工作,因为div没有定义this.href。