2011-11-04 40 views
0

的JQuery的ActionLink和Ajax調用我使用ActionLink的調用下面的方法ASP.NET MVC - 在DIV點擊

@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時複製相同的行爲。我嘗試了幾件事,但他們似乎並沒有工作,我猜測,因爲鏈接調用的網址?

+0

我不明白的quesiton。如果工作正常,問題是什麼? – Jason

+0

嘗試使用div'id'。 –

+0

當我嘗試div ID時它不起作用。 正如我在原帖中所說的,我認爲這是因爲操作鏈接發出了GET請求或其他東西,而點擊div的行爲不會像這樣。 – Tom

回答

0

,因爲你有這個在你的代碼它不會在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> 

在JavaScript:

url: $(this).data("action-url")