0

Steven Sandersonのブログを参照し、リンクのクリックイベントにコントロールの動的挿入を実装しようとしました。今私の場合、それは機能していません。何が悪いのかわかりません。Div(コントロールを含む)の削除は正常に機能しています。しかし、追加コントロールが機能していません。[さらに追加]リンクをクリックすると、新しいページで開きます。同じページに追加のコントロールをレンダリングしません。

私のMainViewコード:

<div id="Div1">
  <% Html.RenderAction("_EditServices", "CRM", new { id = Model.Id });%>
</div>
<div id="editorRows">
  <% Html.RenderAction("_EditInsertServices", "CRM"); %>
</div>
<%= Html.ActionLink("+ Add More Service(s)", "EditAdd", null , new { id = "addItem" })%>

_EditInsertServicesの私のPartiaView:

<div class="editorRow">
<% using (Html.BeginCollectionItem("services"))
  { %>
  NOS:<%:Html.DropDownListFor(model=>model.Id,(SelectList)ViewData["crmServiceType"] as SelectList,"---")%>
  Comment:<%=Html.TextBoxFor(model => model.Comment, new { size = "20" })%>
  <a class="deleteInsertRow">delete</a>
  <% } %>
</div>

私のコントローラーコード:

public ActionResult EditAdd()
{
  ViewData["crmServiceType"] = new SelectList(CRMRequestDL.GetCRM_Service_TypeList().ToArray(), "Id", "ServiceName");
  return View("_EditInsertServices", new CommentedService());
}
public ActionResult _EditInsertServices()
{
  ViewData["crmServiceType"] = new SelectList(CRMRequestDL.GetCRM_Service_TypeList().ToArray(), "Id", "ServiceName");
  return PartialView();
}

脚本:

<script type="text/javascript">
    $("#addItem").click(function () {
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) { $("#editorRows").append(html); }
        });
        return false;
    });
    $("a.deleteInsertRow").live("click", function () {
        $(this).parents("div.editorRow:first").remove();
        return false;
    });
</script>
4

2 に答える 2

1

live()は非推奨になり、jQuery1.9で削除されました

使用するon

  $('#editorRows').on("click", "a.deleteInsertRow",function () {
    $(this).parents("div.editorRow:first").remove();
    return false;
});

イベントについてもっと知りたい場合

于 2013-03-12T08:11:33.890 に答える
0

私はそれを解決しました。実際に$( "#addItem")。click(function(){});を入れました。関数を$(document).ready(function(){});に変換します。機能し、それは正常に動作します。したがって、$( "any class / id")を使用するときは常に、そのメソッドを$(document).ready()関数内に配置する必要があります。

解決策は次のとおりです。

<script type="text/javascript">
$(document).ready(function () {
  $("#addItem").click(function () {
    $.ajax({
    url: this.href,
    cache: false,
    success: function (html) { $("#editorRows").append(html); }});
    return false;
  });
});
</script>
于 2013-03-12T09:30:04.573 に答える